Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 以毫秒精度测量时间_C_Time - Fatal编程技术网

C 以毫秒精度测量时间

C 以毫秒精度测量时间,c,time,C,Time,我的程序将在时间和空间上对不同的排序算法进行竞争。我有足够的空间,但测量时间给我带来了一些麻烦。以下是运行排序的代码: void test(short* n, short len) { short i, j, a[1024]; for(i=0; i<2; i++) { // Loop over each sort algo memused = 0; // Initialize memory marker for(j=0;

我的程序将在时间和空间上对不同的排序算法进行竞争。我有足够的空间,但测量时间给我带来了一些麻烦。以下是运行排序的代码:

void test(short* n, short len) {
  short i, j, a[1024];

  for(i=0; i<2; i++) {         // Loop over each sort algo
    memused = 0;               // Initialize memory marker
    for(j=0; j<len; j++)       // Copy scrambled list into fresh array
      a[j] = n[j];             // (Sorting algos are in-place)
                               // ***Point A***
    switch(i) {                // Pick sorting algo
    case 0:
      selectionSort(a, len);
    case 1:
      quicksort(a, len);
    }
                               // ***Point B***    
    spc[i][len] = memused;     // Record how much mem was used
  }
}
无效测试(短*n,短len){
短i,j,a[1024];
for(i=0;i的分辨率为微秒,易于使用

一对有用的计时器功能是:

static struct timeval tm1;

static inline void start()
{
    gettimeofday(&tm1, NULL);
}

static inline void stop()
{
    struct timeval tm2;
    gettimeofday(&tm2, NULL);

    unsigned long long t = 1000 * (tm2.tv_sec - tm1.tv_sec) + (tm2.tv_usec - tm1.tv_usec) / 1000;
    printf("%llu ms\n", t);
}

时间戳计数器在此处可能会有所帮助:

static unsigned long long rdtsctime() {
    unsigned int eax, edx;
    unsigned long long val;
    __asm__ __volatile__("rdtsc":"=a"(eax), "=d"(edx));
    val = edx;
    val = val << 32;
    val += eax;
    return val;
}
static unsigned long long rdtscttime(){
无符号整数eax,edx;
无符号长val;
__asm_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
val=edx;

val=val您可以使用
getrusage
获得用户+内核的总时间(或只选择一个),如下所示:

#include <sys/time.h>
#include <sys/resource.h>

double get_process_time() {
    struct rusage usage;
    if( 0 == getrusage(RUSAGE_SELF, &usage) ) {
        return (double)(usage.ru_utime.tv_sec + usage.ru_stime.tv_sec) +
               (double)(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec) / 1.0e6;
    }
    return 0;
}

对于测量时间,使用
clock\u gettime
clock\u MONOTONIC
(或
clock\u MONOTONIC\u RAW
(如果可用)。在可能的情况下,避免使用
gettimeofday
。它特别不推荐使用
clock\u gettime
,并且从它返回的时间会受到时间服务器的调整,这可能会影响您的测量。

哪个平台?提到它建议使用POSIX系统,在这种情况下或是合适的(分辨率分别为微秒和纳秒,不一定达到规定的精度)。
man 2 clock_gettime
,或者,如果可以使用C 2011,
int timespec_get(struct timespec*ts,int base);
平台:AMD Athlon 64(运行Debian Linux)假设您在Intel/AMD x86_64平台上,而不是SPARC、PPC、PA-RISC(也可能不是IA-64)。我相信从奔腾4开始,TSC与瞬时CPU频率无关(请参阅Intel-64 SDM第17.12章)。即使使用基于Intel的系统,也不需要特定于CPU的指令来解决上述原始问题。比较排序算法不需要这种粒度(根本不需要)如果做得好。@OliCharlesworth不幸的是没有,但至少它通常提供了一个很好的近似值。当然,这10行左右的代码并不是很好的生产代码,但这也不是我的目标。微秒是
µs
(使用ascii代码230--
'\181'
的八进制),而不是
ms
(表示毫秒)。它通常使用
u
而不是mu:
us
@paddy-Euh…我是否声明了一些反对的内容?哦,很抱歉,我误读了,以为你输出的是微秒。是的,不要使用
gettimeofday
。它会受到时间服务器更新和漂移的影响。
double t_begin, t_end;

t_begin = get_process_time();
// Do some operation...
t_end = get_process_time();

printf( "Elapsed time: %.6f seconds\n", t_end - t_begin );