亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

估計完成 C# 程序所需的時間

估計完成 C# 程序所需的時間

C#
心有法竹 2023-07-22 16:35:02
我正在使用秒表,我想估計從開始到結束需要多長時間。它看起來像一個非常簡單的復合體。我實例化了秒表,啟動它,然后停止它,然后通過編寫一個方法 .elapsed 它應該為我提供從開始到停止所需的時間。不幸的是,它總是為我提供 00:00:00。我是否必須在我的電腦上設置一些適合我的文化的內容?我也嘗試過 .StartNew() 但無濟于事。   Stopwatch stopwatch = new Stopwatch();        stopwatch.Start();        string juniorDevOpsFolder = "JuniorDevOps";        string targetPath = Directory.GetCurrentDirectory();        int endIndex = targetPath.IndexOf(juniorDevOpsFolder);        var juniorDevOpsPath = targetPath.Substring(0, endIndex + juniorDevOpsFolder.Length);        string directory = "Files";        string targetDirectory = Path.Combine(juniorDevOpsPath, directory);        ProcessDirectory(targetDirectory);        stopwatch.Stop();        // Write hours, minutes and seconds.        Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", stopwatch.Elapsed);同樣,輸出為 00:00:00
查看完整描述

4 回答

?
SMILET

TA貢獻1796條經驗 獲得超4個贊

您的程序執行時間不到一秒,因此格式化stopwatch.Elapsed為將 00:00:00 打印到控制臺。嘗試stopwatch.ElapsedMilliseconds而不是格式化stopwatch.Elapsed。


就像是


    Stopwatch stopwatch = new Stopwatch();

    stopwatch.Start();


    //Your business logic


    stopwatch.Stop();

    Console.WriteLine($"Elapsed milliseconds : {stopwatch.ElapsedMilliseconds}" );


查看完整回答
反對 回復 2023-07-22
?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

那段代碼呢:

var watch = new System.Diagnostics.Stopwatch();

        watch.Start();
                // do something what do you want

        watch.Stop();

        Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");


查看完整回答
反對 回復 2023-07-22
?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

可能需要不到一秒鐘的時間。嘗試格式化毫秒。



查看完整回答
反對 回復 2023-07-22
?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

也許只是因為你的程序執行時間不到一秒,這就是為什么你得到零小時、零分鐘和零秒嘗試未格式化的輸出或ElapsesMilliseconds屬性



查看完整回答
反對 回復 2023-07-22
  • 4 回答
  • 0 關注
  • 200 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號