gpt4 book ai didi

c# - C# 中的 CLOCKS_PER_SEC 等价物

转载 作者:行者123 更新时间:2023-11-28 01:42:00 24 4
gpt4 key购买 nike

我在 C++ 中有此代码片段,但很难将其转换为 C#

clock_t now = clock();
myStartTime = start;
myTimeLimit = 5 // in seconds

for (int depth = 2; ((double)(now - myStartTime)) / (double)CLOCKS_PER_SEC < myTimeLimit; depth += 2)
{
//
}

我应该这样做吗?

var now = DateTime.Now;
myStartTime = start;
myTimeLimit = 5;

for (int depth = 2; (now - myStartTime).TotalSeconds < myTimeLimit; depth += 2)
{

}

最佳答案

您可以使用 CancellationTokenSource作为实现这一目标的更好选择。例如

var clt = new CancellationTokenSource(5000);
Task.Run(() => DoSomething(clt.Token));

private static void DoSomething(CancellationToken cltToken)
{
for (int depth = 2; !cltToken.IsCancellationRequested; depth += 2)
{
// . . .
}

if (cltToken.IsCancellationRequested) {
// Time limit reached before finding best move at this depth
}
}

关于c# - C# 中的 CLOCKS_PER_SEC 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46788840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com