Nasıl C Kod Parçacığı Yürütme Zamanı Hesaplamak için
Saniye içinde C kod parçacığı yürütme zamanı hesaplamak için. Windows veya Unix makineler üzerinde çalışıyor olmalı.
Kod aşağıdaki kod bunu yapmak için kullanıyorum. (önce ithalat)
clock_t startTime = clock();
// some code here
// to compute its execution duration in runtime
cout << double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
Bir gibi küçük bir giriş veya kısa ifadeler için ancak = 1, "0 saniye" sonucu. ben bir 0.0000001 saniye falan gibi bir şey olmalı bence.
Java System.nanoTime()
oldukça iyi, bu durumda çalıştığını hatırlıyorum. Ancak işlevsellik C clock()
işlevi aynı alamıyorum .
Bir çözüm var mı?
CEVAP
Yazdığım bu fonksiyonu kullanabilirsiniz. GetTimeMs64()
ve milisaniye unix epoch beri geçen sayısı sistem saatini kullanarak döndürür time(NULL)
dışında milisaniye gibi sadece Ara.
Hem windows hem de linux üzerinde çalışır; iş parçacığı güvenlidir.
Parçalı windows üzerinde 15 ms; linux üzerinde uygulama bağımlı, ama genellikle 15 ms de olduğunu unutmayın.
#ifdef _WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#include <ctime>
#endif
/* Remove if already defined */
typedef long long int64; typedef unsigned long long uint64;
/* Returns the amount of milliseconds elapsed since the UNIX epoch. Works on both
* windows and linux. */
uint64 GetTimeMs64()
{
#ifdef _WIN32
/* Windows */
FILETIME ft;
LARGE_INTEGER li;
/* Get the amount of 100 nano seconds intervals elapsed since January 1, 1601 (UTC) and copy it
* to a LARGE_INTEGER structure. */
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
uint64 ret = li.QuadPart;
ret -= 116444736000000000LL; /* Convert from file time to UNIX epoch time. */
ret /= 10000; /* From 100 nano seconds (10^-7) to 1 millisecond (10^-3) intervals */
return ret;
#else
/* Linux */
struct timeval tv;
gettimeofday(&tv, NULL);
uint64 ret = tv.tv_usec;
/* Convert from micro seconds (10^-6) to milliseconds (10^-3) */
ret /= 1000;
/* Adds the seconds (10^0) after converting them to milliseconds (10^-3) */
ret = (tv.tv_sec * 1000);
return ret;
#endif
}
Nasıl windows komut yürütme zamanı ölç...
Yürütme zamanı milisaniye aşağı almak ...
Nasıl geri çağrıları ile javascript ko...
Çözmek için nasıl "Eklenti yürütm...
Nasıl PHP kullanarak iki tarih arasınd...