Windows via C/C++£ºÏ̵߳ÄÖ´ÐÐʱ¼ä
¼ÆËãÏß³ÌÖ´ÐÐijÏîÈÎÎñÏûºÄµÄʱ¼äʱ£¬Ðí¶à¿ª·¢ÈËÔ±»áµ÷ÓÃGetTickCount/GetTickCount64±àдÈçϵĴúÂ룺
// Get the current time (start time)
ULONGLONG qwStartTime = GetTickCount64();
// Perform complex algorithm here
// Subtract start time from current time to get duration
ULONGLONG dwElapsedTime = GetTickCount64() - qwStartTime;
Õâ¶Î´úÂë¼ÙÉ赱ǰÏ̲߳»»á±»Öжϡ£È»¶øÔÚWindowsÕâÑùµÄ»ùÓÚÓÅÏȼ¶µÄ²Ù×÷ϵͳÖУ¬¿ª·¢ÈËÔ±ÎÞ·¨µÃÖªÏ̱߳»µ÷¶ÈµÄ׼ȷʱ¼ä¡£µ±Ïß³ÌÔÚÖ´ÐÐÈÎÎñÖб»ÖжÏʱ£¬Ê¹ÓÃÉÏÃæµÄ·½·¨¸ù±¾ÎÞ·¨»ñµÃÏß³ÌËùÏûºÄµÄʱ¼ä¡£ÎÒÃÇÐèÒªÒ»¸ö¿ÉÒÔ·µ»ØÏß³ÌÏûºÄµÄCPUʱ¼ä£¨¼È±»µ÷¶Èʱ¼ä£©µÄº¯Êý£¬ÐÒÔ˵ÄÊÇ£¬ÔÚVista֮ǰµÄ²Ù×÷ϵͳÒѾÌṩÁËGetThreadTimes×öµ½ÕâÒ»µã£º
BOOL GetThreadTimes(HANDLE hThread,
PFILETIME pftCreationTime,
PFILETIME pftExitTime,
PFILETIME pftKernelTime,
PFILETIME pftUserTime);
GetThreadTimeº¯Êý»á½«Ïß³ÌÏà¹ØµÄʱ¼äÐÅϢдÈëΪÆä´«µÝµÄPFILETIME²ÎÊýÖУ¬¸÷¸ö²ÎÊý·µ»ØÖµµÄº¬ÒåÈçϱíËùʾ£º
²ÎÊý
ÒâÒå
pftCreationTime
´Ó1601Äê1ÔÂ1ºÅÁ賿¿ªÊ¼µ½Ö¸¶¨Ï̱߳»´´½¨Ê±µÄʱ¼ä£¬ÒÔ100ÄÉÃëΪµ¥Î»
pftExitTime
´Ó1601Äê1ÔÂ1ºÅÁ賿¿ªÊ¼µ½Ö¸¶¨Ï߳̽áÊøËù»¨µÄʱ¼ä£¬ÒÔ100ÄÉÃëΪµ¥Î»£¬¼ÙÈçÏß³ÌÉÐδÖÕÖ¹£¬Ôò¸Ãֵ䶨Òå
pftKernelTime
Ïß³ÌÔÚÄÚºËģʽÏÂÔËÐÐËù»¨µÄʱ¼ä£¬ÒÔ100ÄÉÃëΪµ¥Î»
pftUserTime
Ïß³ÌÔÚÓû§Ä£Ê½ÏÂÔËÐÐËù»¨µÄʱ¼ä£¬ÒÔ100ÄÉÃëΪµ¥Î»
ʹÓÃGetThreadTimes¿ÉÒÔ¼ÆËãÏß³ÌËùÏû·ÑµÄCPUʱ¼ä£¬±ÈÈçÏÂÃæµÄ´úÂ룺
__int64 FileTimeToQuadWord(PFILETIME pft) {
return (Int64ShllMod32(ptf->dwHighDateTime, 32) | ptf->dwLowDateTime);
}
void PerformLongOperation() {
FILETIME ftKernelTimeStart, ftKernelTimeEnd;
FILETIME ftUserTiimeStart, ftUserTimeEnd;
FILETIME ftDummy;
__int64 qwKernelTimeElapsed, qwUserTimeElapsed, qwTotalTimeElapsed;
// Get starting times
GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy, &ftKernelTimeStart, &ftUserTimeStart);
// Perform complex algorithm here
...
// Get the ending times
GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy, &ftKernelTimeEnd, &ftUserTimeEnd);
// Get the elapsed kernel and user times by c
Ïà¹ØÎĵµ£º
ÕâƪÎÄÕÂÊÇʹÓÃSQLite C/C++½Ó¿ÚµÄÒ»¸ö¸ÅÒª½éÉܺÍÈëÃÅÖ¸ÄÏ¡£
ÓÉÓÚÔçÆÚµÄSQLiteÖ»Ö§³Ö5¸öC/C++½Ó¿Ú£¬Òò¶ø·Ç³£ÈÝÒ×ѧϰºÍʹÓ㬵«ÊÇËæ×ÅSQLite¹¦ÄܵÄÔöÇ¿£¬ÐµÄC/C++½Ó¿Ú²»¶ÏµÄÔö¼Ó½øÀ´£¬µ½ÏÖÔÚÓг¬¹ý150¸ö²»Í¬µÄAPI½Ó¿Ú¡£ÕâÍùÍùʹ³õѧÕßÍû¶øÈ´²½¡£ÐÒÔ˵ÄÊÇ£¬´ó¶àÊýSQLiteÖеÄC/C++½Ó¿ÚÊÇרÓõģ¬Òò¶øºÜÉÙ±»Ê¹Óõ½¡£¾¡¹ÜÓÐÕâ ......
LinuxÏ»ñµÃϵͳʱ¼äµÄCÓïÑÔµÄʵÏÖ·½·¨
#include<time.h> //CÓïÑÔµÄÍ·Îļþ
#include<stdio.h> //C﵀I/O
void main()
{
time_t now; //ʵÀý»¯time_t½á¹¹
struct tm *timenow; //ʵÀý»¯tm½á¹¹Ö¸Õë
time(&now);
//timeº¯Êý¶ÁÈ¡ÏÖÔÚµ ......
»ùÓÚÓÅÏȼ¶µÄ²Ù×÷ϵͳ£¨preemptive operating system£©±ØÐëʹÓÃijÖÖËã·¨ÒÔ¾ö¶¨CPUÈçºÎµ÷¶ÈϵͳÖеÄỊ̈߳¬±¾Õ½«ÌÖÂÛWindows VistaʹÓõĵ÷¶ÈËã·¨¡£ ÎÒÃÇ֮ǰÌÖÂÛ¹ýÏ̵߳ÄÉÏÏÂÎĽṹCONTEXT£¬ÆäÖк¬ÓÐÏß³Ì×îºóÒ»´ÎÖ´ÐÐʱCPU¼Ä´æÆ÷×éµÄ״̬¡£ ²Ù×÷ϵͳÿ20ºÁÃë¼ì²éϵͳÖÐËùÓÐÏß³ÌÄں˶ÔÏóµÄ״̬£¬ÆäÖÐһЩ¶ÔÏó±»ÈÏΪÊǿɵ÷¶È£ ......
º¯Êý - ÉùÃ÷¡¢¶¨Òå¡¢µ÷ÓÃ
¡¡¡¡1. Èç¹ûº¯ÊýûÓÐÉùÃ÷, Ó¦¸ÃÔÚµ÷ÓÃÇ°¶¨Òå:
#include¡¡<stdio.h>
/*¡¡¶¨ÒåÇó´óÖµº¯Êý¡¡*/
int¡¡MAX(int¡¡x,¡¡int¡¡y)¡¡{
¡¡¡¡if¡¡(x¡¡>¡¡y)
¡¡¡¡¡¡¡¡return¡¡x;
¡¡¡¡else
¡¡¡¡¡¡¡¡return¡¡y;
}¡¡
/*¡¡¶¨ÒåÇóСֵº¯Êý¡¡*/
int¡¡MIN(int¡¡x,¡¡int¡¡y)¡¡{
¡¡¡¡return¡¡x¡¡&l ......