毫秒定时C++; 我想对我编写的一些C++函数的实时性能进行计时。如何以毫秒为单位获取计时 // ///////////////////////////////////////////////////////////////////////////// uint64_t mynamespace::get_system_microsecond(void) { uint64_t total_ns = dtb::get_system_nanosecond(); // see below uint64_t ret_val = total_ns / NSPUS; // NanoSecondsPerMicroSeconds return(ret_val); } // ///////////////////////////////////////////////////////////////////////////// uint64_t mynamespace::get_system_nanosecond(void) { //struct timespec { __time_t tv_sec; long int tv_nsec; }; -- total 8 bytes struct timespec ts; // CLOCK_REALTIME - system wide real time clock int status = clock_gettime(CLOCK_REALTIME, &ts); dtb_assert(0 == status); // to 8 byte from 4 byte uint64_t uli_nsec = ts.tv_nsec; uint64_t uli_sec = ts.tv_sec; uint64_t total_ns = uli_nsec + (uli_sec * NSPS); // nano-seconds-per-second return(total_ns); }
我知道如何通过毫秒定时C++; 我想对我编写的一些C++函数的实时性能进行计时。如何以毫秒为单位获取计时 // ///////////////////////////////////////////////////////////////////////////// uint64_t mynamespace::get_system_microsecond(void) { uint64_t total_ns = dtb::get_system_nanosecond(); // see below uint64_t ret_val = total_ns / NSPUS; // NanoSecondsPerMicroSeconds return(ret_val); } // ///////////////////////////////////////////////////////////////////////////// uint64_t mynamespace::get_system_nanosecond(void) { //struct timespec { __time_t tv_sec; long int tv_nsec; }; -- total 8 bytes struct timespec ts; // CLOCK_REALTIME - system wide real time clock int status = clock_gettime(CLOCK_REALTIME, &ts); dtb_assert(0 == status); // to 8 byte from 4 byte uint64_t uli_nsec = ts.tv_nsec; uint64_t uli_sec = ts.tv_sec; uint64_t total_ns = uli_nsec + (uli_sec * NSPS); // nano-seconds-per-second return(total_ns); },c++,time,C++,Time,我知道如何通过 start=clock() diff=(clock()-start)/(double) CLOCKS_PER_SEC cout<<diff // ///////////////////////////////////////////////////////////////////////////// uint64_t mynamespace::get_system_microsecond(void) { uint64_t total_ns = d
start=clock()
diff=(clock()-start)/(double) CLOCKS_PER_SEC
cout<<diff
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_microsecond(void)
{
uint64_t total_ns = dtb::get_system_nanosecond(); // see below
uint64_t ret_val = total_ns / NSPUS; // NanoSecondsPerMicroSeconds
return(ret_val);
}
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_nanosecond(void)
{
//struct timespec { __time_t tv_sec; long int tv_nsec; }; -- total 8 bytes
struct timespec ts;
// CLOCK_REALTIME - system wide real time clock
int status = clock_gettime(CLOCK_REALTIME, &ts);
dtb_assert(0 == status);
// to 8 byte from 4 byte
uint64_t uli_nsec = ts.tv_nsec;
uint64_t uli_sec = ts.tv_sec;
uint64_t total_ns = uli_nsec + (uli_sec * NSPS); // nano-seconds-per-second
return(total_ns);
}
start=clock()
diff=(clock()-start)/(double)每秒时钟
cout尝试diff=(clock()-start)*1000.0/时钟/秒
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_microsecond(void)
{
uint64_t total_ns = dtb::get_system_nanosecond(); // see below
uint64_t ret_val = total_ns / NSPUS; // NanoSecondsPerMicroSeconds
return(ret_val);
}
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_nanosecond(void)
{
//struct timespec { __time_t tv_sec; long int tv_nsec; }; -- total 8 bytes
struct timespec ts;
// CLOCK_REALTIME - system wide real time clock
int status = clock_gettime(CLOCK_REALTIME, &ts);
dtb_assert(0 == status);
// to 8 byte from 4 byte
uint64_t uli_nsec = ts.tv_nsec;
uint64_t uli_sec = ts.tv_sec;
uint64_t total_ns = uli_nsec + (uli_sec * NSPS); // nano-seconds-per-second
return(total_ns);
}
我们的想法是将时钟数乘以1000,这样在得到2(秒)之前,现在可以得到2000(毫秒)。在Linux中,请查看clock\u gettime()
。从本质上讲,它可以为您提供从任意点开始经过的时间,单位为纳秒(这对您来说已经足够好了)
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_microsecond(void)
{
uint64_t total_ns = dtb::get_system_nanosecond(); // see below
uint64_t ret_val = total_ns / NSPUS; // NanoSecondsPerMicroSeconds
return(ret_val);
}
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_nanosecond(void)
{
//struct timespec { __time_t tv_sec; long int tv_nsec; }; -- total 8 bytes
struct timespec ts;
// CLOCK_REALTIME - system wide real time clock
int status = clock_gettime(CLOCK_REALTIME, &ts);
dtb_assert(0 == status);
// to 8 byte from 4 byte
uint64_t uli_nsec = ts.tv_nsec;
uint64_t uli_sec = ts.tv_sec;
uint64_t total_ns = uli_nsec + (uli_sec * NSPS); // nano-seconds-per-second
return(total_ns);
}
请注意,它是由POSIX标准指定的,因此您可以在Unix派生系统上使用它。注意:
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_microsecond(void)
{
uint64_t total_ns = dtb::get_system_nanosecond(); // see below
uint64_t ret_val = total_ns / NSPUS; // NanoSecondsPerMicroSeconds
return(ret_val);
}
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_nanosecond(void)
{
//struct timespec { __time_t tv_sec; long int tv_nsec; }; -- total 8 bytes
struct timespec ts;
// CLOCK_REALTIME - system wide real time clock
int status = clock_gettime(CLOCK_REALTIME, &ts);
dtb_assert(0 == status);
// to 8 byte from 4 byte
uint64_t uli_nsec = ts.tv_nsec;
uint64_t uli_sec = ts.tv_sec;
uint64_t total_ns = uli_nsec + (uli_sec * NSPS); // nano-seconds-per-second
return(total_ns);
}
在我的戴尔桌面上,这相当快。。。
UbuntuBogomips峰值为5210
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_microsecond(void)
{
uint64_t total_ns = dtb::get_system_nanosecond(); // see below
uint64_t ret_val = total_ns / NSPUS; // NanoSecondsPerMicroSeconds
return(ret_val);
}
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_nanosecond(void)
{
//struct timespec { __time_t tv_sec; long int tv_nsec; }; -- total 8 bytes
struct timespec ts;
// CLOCK_REALTIME - system wide real time clock
int status = clock_gettime(CLOCK_REALTIME, &ts);
dtb_assert(0 == status);
// to 8 byte from 4 byte
uint64_t uli_nsec = ts.tv_nsec;
uint64_t uli_sec = ts.tv_sec;
uint64_t total_ns = uli_nsec + (uli_sec * NSPS); // nano-seconds-per-second
return(total_ns);
}
时间(0)大约需要80纳秒(2.4秒内有3000万次呼叫)
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_microsecond(void)
{
uint64_t total_ns = dtb::get_system_nanosecond(); // see below
uint64_t ret_val = total_ns / NSPUS; // NanoSecondsPerMicroSeconds
return(ret_val);
}
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_nanosecond(void)
{
//struct timespec { __time_t tv_sec; long int tv_nsec; }; -- total 8 bytes
struct timespec ts;
// CLOCK_REALTIME - system wide real time clock
int status = clock_gettime(CLOCK_REALTIME, &ts);
dtb_assert(0 == status);
// to 8 byte from 4 byte
uint64_t uli_nsec = ts.tv_nsec;
uint64_t uli_sec = ts.tv_sec;
uint64_t total_ns = uli_nsec + (uli_sec * NSPS); // nano-seconds-per-second
return(total_ns);
}
时间(0)允许我测量
clock_gettime(),每次调用大约需要1.3 u秒(3秒内220万次)
(我不记得每个时间步有多少纳秒)
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_microsecond(void)
{
uint64_t total_ns = dtb::get_system_nanosecond(); // see below
uint64_t ret_val = total_ns / NSPUS; // NanoSecondsPerMicroSeconds
return(ret_val);
}
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_nanosecond(void)
{
//struct timespec { __time_t tv_sec; long int tv_nsec; }; -- total 8 bytes
struct timespec ts;
// CLOCK_REALTIME - system wide real time clock
int status = clock_gettime(CLOCK_REALTIME, &ts);
dtb_assert(0 == status);
// to 8 byte from 4 byte
uint64_t uli_nsec = ts.tv_nsec;
uint64_t uli_sec = ts.tv_sec;
uint64_t total_ns = uli_nsec + (uli_sec * NSPS); // nano-seconds-per-second
return(total_ns);
}
因此,我通常使用以下方法,调用时间大约为3秒
// ////////////////////////////////////////////////////////////////////////////
void measuring_something_duration()
...
uint64_t start_us = dtb::get_system_microsecond();
do_something_for_about_3_seconds()
uint64_t test_duration_us = dtb::get_system_microsecond() - start_us;
uint64_t test_duration_ms = test_duration_us / 1000;
...
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_microsecond(void)
{
uint64_t total_ns = dtb::get_system_nanosecond(); // see below
uint64_t ret_val = total_ns / NSPUS; // NanoSecondsPerMicroSeconds
return(ret_val);
}
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_nanosecond(void)
{
//struct timespec { __time_t tv_sec; long int tv_nsec; }; -- total 8 bytes
struct timespec ts;
// CLOCK_REALTIME - system wide real time clock
int status = clock_gettime(CLOCK_REALTIME, &ts);
dtb_assert(0 == status);
// to 8 byte from 4 byte
uint64_t uli_nsec = ts.tv_nsec;
uint64_t uli_sec = ts.tv_sec;
uint64_t total_ns = uli_nsec + (uli_sec * NSPS); // nano-seconds-per-second
return(total_ns);
}
哪些用户使用这些功能
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_microsecond(void)
{
uint64_t total_ns = dtb::get_system_nanosecond(); // see below
uint64_t ret_val = total_ns / NSPUS; // NanoSecondsPerMicroSeconds
return(ret_val);
}
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_nanosecond(void)
{
//struct timespec { __time_t tv_sec; long int tv_nsec; }; -- total 8 bytes
struct timespec ts;
// CLOCK_REALTIME - system wide real time clock
int status = clock_gettime(CLOCK_REALTIME, &ts);
dtb_assert(0 == status);
// to 8 byte from 4 byte
uint64_t uli_nsec = ts.tv_nsec;
uint64_t uli_sec = ts.tv_sec;
uint64_t total_ns = uli_nsec + (uli_sec * NSPS); // nano-seconds-per-second
return(total_ns);
}
请记住链接-lrt它的可能副本令人印象深刻,最近有多少问题使用clock()
问同样的问题。要测量时间,您应该使用gettimeofday()
或clock\u gettime()
^我认为有一种比gettimeofday()、clock\u gettime()更新、更好的替代方法。。。下面提到了以太灵。我很惊讶gettimeofday()也即将出炉。我不想成为一个彻头彻尾的恶棍,但为什么这会增加选票呢?正如所指出的,这个问题以前已经被问过很多次了。+1,特别是CLOCK\u PROCESS\u CPUTIME\u ID
将取代CLOCK()
@Jerry Coffin您当然是对的,但OP确实说他们正在使用Linux,自1994年以来Linux就有CLOCK\u PROCESS\u CPUTIME\u ID和CLOCK\u THREAD\u CPUTIME\u ID,看起来是这样。@Cubbi:oops——我没有读到我应该读的那么深入的问题,并且错过了使用Linux的参考。我的错误。
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_microsecond(void)
{
uint64_t total_ns = dtb::get_system_nanosecond(); // see below
uint64_t ret_val = total_ns / NSPUS; // NanoSecondsPerMicroSeconds
return(ret_val);
}
// /////////////////////////////////////////////////////////////////////////////
uint64_t mynamespace::get_system_nanosecond(void)
{
//struct timespec { __time_t tv_sec; long int tv_nsec; }; -- total 8 bytes
struct timespec ts;
// CLOCK_REALTIME - system wide real time clock
int status = clock_gettime(CLOCK_REALTIME, &ts);
dtb_assert(0 == status);
// to 8 byte from 4 byte
uint64_t uli_nsec = ts.tv_nsec;
uint64_t uli_sec = ts.tv_sec;
uint64_t total_ns = uli_nsec + (uli_sec * NSPS); // nano-seconds-per-second
return(total_ns);
}