衡量代码的吞吐量和延迟

时间:2016-07-13 21:16:58

标签: c multithreading performance linked-list profiling

我为LinkedList编写了一个多线程C代码。我正在尝试测量代码的吞吐量和延迟。为了测量吞吐量,这是我的代码

clock_t begin = clock();    
    pthread_create (&t1, NULL, thread1, (void *)head);
    pthread_create (&t2, NULL, thread2, (void *)head);
    pthread_create (&t3, NULL, thread3, (void *)head);
    pthread_create (&t4, NULL, thread4, (void *)head);
    pthread_join (t1, NULL);
    pthread_join (t2, NULL);
    pthread_join (t3, NULL);
    pthread_join (t4, NULL);
clock_t end = clock();

延迟时间如下

void * thread1(void * args)

{
  clock_t begin = clock();

 /* LinkedList Operations */

  clock_t begin = clock();
}

我是否正确测量了这两个参数,还是有其他方法可以做到这一点?

提前致谢!

1 个答案:

答案 0 :(得分:1)

我的个人偏好是这样的:

struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
sleep(1);
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
const uint64_t ns = (end.tv_sec * 1000000000 + end.tv_nsec) - (start.tv_sec * 1000000000 + start.tv_nsec);
printf("elapsed %7.02f ms (%lu ns)\n", ns / 1000000.0, ns);
相关问题