对于 C# Stopwatch ElapsedTick 的疑惑

发布于 2022-09-12 03:12:28 字数 2036 浏览 22 评论 0

A*算法的部分代码

    MLOG.time("newCostDealer");
    MLOG.time("newCostDealer - newCost");
    float newCost = cost_so_far[cur]+ heuristic(next.Pos, end.Pos);
    MLOG.timeEnd("newCostDealer - newCost");

    MLOG.time("newCostDealer - 0");
    bool tmp2 = !cost_so_far.ContainsKey(next);
    MLOG.timeEnd("newCostDealer - 0");

    MLOG.time("newCostDealer - temp3");
    bool tmp3 = false;
    if (!tmp2) {
        tmp3 = newCost < cost_so_far[next];
    }
    MLOG.timeEnd("newCostDealer - temp3");

    if (tmp2 || tmp3)
    {
        MLOG.time("newCostDealer - 1");
        frontier.put(next, newCost);
        MLOG.timeEnd("newCostDealer - 1");

        MLOG.time("newCostDealer - 2");
        cost_so_far[next] = newCost;
        // MLOG.info("came_from[next] = cur," + next.ToString() + " " + cur.ToString());
        came_from[next] = cur;
        MLOG.timeEnd("newCostDealer - 2");
    }

    MLOG.timeEnd("newCostDealer");

运行结果:

tIfWA1.png

求教,为啥 newCostDealer ticks 和其它记录加起来差那么多丫?!


使用的记录方法

    static public void time (string key) {
        if (timeKeyHub.ContainsKey (key)) {
            Debug.LogError ("MLOG.cs time key already exist");
        } else {
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch ();
            timeKeyHub.Add (key, sw);
            sw.Start ();
        }
    }
    static public void timeEnd (string key) {
        if (timeKeyHub.ContainsKey (key)) {
            System.Diagnostics.Stopwatch sw;
            timeKeyHub.TryGetValue (key, out sw);
            sw.Stop();
            Debug.Log (string.Format ("<color=blue>{0}</color> {1} ms {2} ticks", key, sw.ElapsedMilliseconds, sw.ElapsedTicks));
            // BIRDTODO:用池子
            sw = null;
            timeKeyHub.Remove (key);
        } else {
            Debug.LogError ("MLOG.cs timeend key not exist");
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文