CLOCKS_PER_SEC C# 中的等效项
CLOCKS_PER_SEC equivalent in C#
我在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
}
}
相关文章:
- DrawIndexedInstanced 具有不同的 Index Count per Instance (Directx
- 继承构造函数(ISO 2011 SEC 12.9第7段)
- 如何做 boost::asio::spawn 与 io_service-per CPU.
- 为整个Markdown文档设置默认的PER代码语言
- D3D11中没有三个组件8bits-per-per-per-per-per-per-per-per-dxgi纹理格式
- "State pattern" vs "one member function per state" ?
- 具有 SVM、EM 和其他具有"weights per sample"的方法的库,例如
- BOOST LOCAL_DATE_TIME:如何设置特定时间信息(小时,最小值和SEC)
- Visual Studio 2013 中"per file"自定义生成步骤在哪里?
- Websocket握手Sec-Webocket Accept标头值不正确
- Is struct A { int a[2] = {1, 2}; }; legal as per C++11?
- 如何解释浏览器的服务器握手的 Sec-websocket-key ?
- 将 int** 转换为"pointer to a two-dimensional array of integers with fixed number of elements per column"