确定处理例程所需的时间(例如函数的过程)最好和最准确的方法是什么?
我问,因为我目前正在尝试优化我的应用程序中的一些函数,当我测试更改时,如果有任何改进,只需通过查看就很难确定.因此,如果我能够返回一个准确或接近准确的处理例程的时间,那么我可以更清楚地了解代码是否有任何变化.
我考虑过使用GetTickCount,但我不确定这是否接近准确?
有一个可恢复的函数/过程来计算例程的时间是有用的,并使用它像这样:
// < prepare for calcuation of code
...
ExecuteSomeCode; // < code to test
...
// < stop calcuating code and return time it took to process
Run Code Online (Sandbox Code Playgroud)
我期待听到一些建议.
谢谢.
克雷格.
我有一个关于GetTickCount函数的问题,我在我的代码中有两个调用此函数,它们之间有几个命令,两个调用中的函数都返回相同的计数.即
var1 = GetTickCount();
code
:
:
var2 = GetTickCount();
Run Code Online (Sandbox Code Playgroud)
var1和var2中包含相同的值.
有人可以帮忙吗?
unsigned int Tick = GetTickCount();
Run Code Online (Sandbox Code Playgroud)
该代码仅在 Windows 上运行,但我想使用 C++ 标准库,以便它可以在其他地方运行。
我搜索过std::chrono,但找不到类似的功能GetTickCount()。
你知道我应该使用什么吗std::chrono?
默认情况下,GetTickCount 和 timeGetTime 具有相同的分辨率 - 15.625ms,但是在我调用 timeBeginPeriod(1) 之后,GetTickCount 仍然每 15.625 ms 更新一次,而 timeGetTime 每 1ms 更新一次,这是为什么?
在等待计时器中的错误?,作者提到:

我想知道:为什么GetTickCount和timeGetTime来自同一个RTC,但有两种分辨率?
谢谢!
我不太擅长 C++,更擅长 C# 和 PHP。我被分配了一个项目,需要我使用GetTickCount并连接到一个应用程序。我需要一些帮助,因为由于某种原因它没有按计划工作...这是挂钩的代码,我知道它有效,因为我以前在项目中使用过它。我唯一不太确定的是GetTickCount它的一部分。我尝试GetTickCount64认为这可以解决我的问题(它没有使我注入的内容崩溃),但发现它根本不起作用,所以它没有崩溃。
bool APIENTRY DllMain(HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
{
switch(dwReason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hDll);
CreateThread(0,0, (LPTHREAD_START_ROUTINE)KeyHooks, 0, 0, 0);
GetTickCount_orig = (DWORD (__stdcall *)(void))DetourFunction((PBYTE)GetProcAddress(GetModuleHandle("kernel32.dll"), "GetTickCount"), (PBYTE)GetTickCount_hooked);
case DLL_PROCESS_DETACH:
DetourRemove((PBYTE)GetProcAddress(GetModuleHandle("kernel32.dll"), "GetTickCount"), (PBYTE)GetTickCount_hooked);
break;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
这是用于的其余代码GetTickCount
DWORD oldtick=0;
DWORD (WINAPI *GetTickCount_orig)(void);
DWORD WINAPI GetTickCount_hooked(void)
{
if(oldtick==0)
{
oldtick=(*GetTickCount_orig)();
return oldtick;
}
DWORD factor;
DWORD ret;
ret = (*GetTickCount_orig)();
factor = 3.0;
DWORD newret;
newret = ret+((oldtick-ret)*(factor-1));
oldtick=ret; …Run Code Online (Sandbox Code Playgroud) 我想从kernel32.dll库声明一个名为GetTickCount64的外部函数.据我所知,它仅在Vista和后来的Windows版本中定义.这意味着当我定义函数时如下:
function GetTickCount64: int64; external kernel32 name 'GetTickCount64';
Run Code Online (Sandbox Code Playgroud)
由于应用程序启动时生成错误,我肯定无法在以前版本的Windows上运行我的应用程序.
这个问题有解决方法吗?假设我不想在不存在时包含该函数,然后在我的代码中使用一些替换函数.怎么做?是否有任何编译器指令可以帮助?我猜这个定义必须被这样的指令所包围,我还必须使用一些指令,无论我在哪里使用GetTickCount64功能,对吧?
我们将不胜感激.提前致谢.
马里乌什.
我想知道系统最后一次启动的时间.
Environment.TickCount会起作用,但由于int的限制,它会在48-49天后破坏.
这是我一直在使用的代码:
Environment.TickCount & Int32.MaxValue
Run Code Online (Sandbox Code Playgroud)
有人知道长型返回吗?
我用它来知道系统的空闲时间:
public static int GetIdleTime()
{
return (Environment.TickCount & Int32.MaxValue)- (int)GetLastInputTime();
}
/// <summary>
/// Get the last input time from the input devices.
/// Exception:
/// If it cannot get the last input information then it throws an exception with
/// the appropriate message.
/// </summary>
/// <returns>Last input time in milliseconds.</returns>
public static uint GetLastInputTime()
{
LastInputInfo lastInPut = new LastInputInfo();
lastInPut.BlockSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
if (!GetLastInputInfo(ref lastInPut))
{
throw new Exception(GetLastError().ToString()); …Run Code Online (Sandbox Code Playgroud) 我正在制作FPS(第一人称射击游戏)游戏,我想在游戏中显示玩家的ping(连接延迟).但是这样做的最佳方法是什么?首先我想使用GetTickCount64,但Get Tick Count不准确:
"GetTickCount64函数的分辨率仅限于系统计时器的分辨率,通常在10毫秒到16毫秒的范围内."
我有一个想法,使用time.h来查看1秒内有多少Tick Ticknts.但我认为这不是最好的解决方案.
有人可以帮我弄这个吗?
编辑:我正在制作Windows游戏.(谢谢放松和Lefteris提到我忘了记下来了)
绝对值是否保护Environment.TickCount包装中的以下代码?
If Math.Abs((Environment.TickCount And Int32.MaxValue) - StartTime) >= Interval Then
StartTime = Environment.TickCount And Int32.MaxValue ' set up next interval
...
...
...
End If
Run Code Online (Sandbox Code Playgroud)
有没有更好的防范Environment.TickCount环绕的方法?
(这是.NET 1.1.)
编辑 - 根据Microsoft Environment.TickCount帮助修改代码.
我一直在创建一个空闲程序,以便在鼠标和键盘处于非活动状态时在几分钟内计算.这是我到目前为止:
using namespace std;
while(true)
{
GetLastInputInfo(&last_info);
tickCount = GetTickCount();
int minutes = (tickCount - last_info.dwTime) / 60000;
count++;
if((minutes >= 1) && (count%3000==0))
{
ifstream in("in.txt");
ofstream out("out.txt");
float sum;
in >> sum;
sum = sum++;
out << sum;
out << in.rdbuf();
out.close();
in.close();
}
std::cout << "Idle Time: " << minutes << " minutes." << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行它闲置一分钟时,"总和"表示它是1,然后我关闭程序并再次打开它一分钟,"总和"说它是2.我关闭程序并再打开一分钟它又回到了1.为什么会发生这种情况?
我正在创建一个我需要运行的程序,然后等待10分钟再继续
procedure firstTimeRun(const document: IHTMLDocument2);
var
fieldValue : string;
StartTime : Dword;
begin
StartTime := GetTickCount();
WebFormSetFieldValue(document, 0, 'Username', '*******');
WebFormSetFieldValue(document, 0, 'Password', '*******');
WebFormSubmit(document, 0);
if (GetTickCount() >= StartTime+ 600000) then
begin
SecondRun();
end;
end;
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我到达if语句时,它会检查它是不是真的并继续我如何让它保持并等到语句为真?
gettickcount ×11
c++ ×4
delphi ×3
.net ×2
timer ×2
c ×1
c# ×1
c#-4.0 ×1
c++-chrono ×1
c++11 ×1
connection ×1
detours ×1
external ×1
frame-rate ×1
function ×1
hook ×1
ifstream ×1
ofstream ×1
optimization ×1
performance ×1
ping ×1
rollover ×1
std ×1
time ×1
vb.net ×1
wait ×1
winapi ×1
windows ×1