衡量计算量的一个方法, CPU 使用时间

重要接口1:获取本线程的 CPU 使用时间 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t);

重要接口2:获取本进程的 CPU 使用时间 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t);

CPU  使用时间可以理解为代码实际在运算的时间。

#include <cstdlib>

#include <iostream>

#include <ostream>

#include <stdexcept>

#include <stdio.h>

#include <time.h>



#include <thread>

#include <chrono>

#include <vector>



void compute()

{

std::cout << "enter thread: " << std::this_thread::get_id() << std::endl;

const std::chrono::time_point<std::chrono::steady_clock> start =

std::chrono::steady_clock::now();

long a = 0;

for (long i = 0; i < 10000000; i++) {

for (long j = 0; j < 50; j++) {

a += i;

}

}

std::this_thread::sleep_for(std::chrono::seconds(20));

const std::chrono::time_point<std::chrono::steady_clock> end =

std::chrono::steady_clock::now();

auto dt_sec = std::chrono::duration_cast<std::chrono::duration<double>>(end - start).count();

std::cout << a << " thread " << std::this_thread::get_id() << " run wall time: " << dt_sec << std::endl;



timespec t;

clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t);

std::cout << a << " thread " << std::this_thread::get_id() << " run cpu time: " << t.tv_sec + 1e-9* t.tv_nsec << std::endl;

}



int main()

{

std::cout << "hello world from t1 main!" << std::endl;

const std::chrono::time_point<std::chrono::steady_clock> start =

std::chrono::steady_clock::now();



std::vector<std::thread> v_t;

for (int i = 0; i < 5; i++) {

v_t.push_back(std::thread(compute));

}



for (int i = 0; i < 5; i++) {

v_t[i].join();

}



const std::chrono::time_point<std::chrono::steady_clock> end =

std::chrono::steady_clock::now();

auto dt_sec = std::chrono::duration_cast<std::chrono::duration<double>>(end - start).count();

std::cout << " main run wall time: " << dt_sec << std::endl;



timespec t;

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t);

std::cout << "main run cpu time: " << t.tv_sec + 1e-9* t.tv_nsec << std::endl;

std::cout << "return main" << std::endl;

return 0;

}

输出

```C++

hello world from t1 main!
enter thread: 140318356154112
enter thread: 140318347761408
enter thread: 140318339368704
enter thread: 140318330976000
enter thread: 140318322583296
2499999750000000 thread 140318347761408 run wall time: 20.7371
2499999750000000 thread 140318347761408 run cpu time: 0.736961
2499999750000000 thread 140318356154112 run wall time: 2499999750000000 thread 20.7392
2499999750000000 thread 140318356154112 run cpu time: 0.739221
140318339368704 run wall time: 20.7392
2499999750000000 thread 140318339368704 run cpu time: 0.739155
2499999750000000 thread 140318322583296 run wall time: 20.7393
2499999750000000 thread 140318322583296 run cpu time: 0.739314
2499999750000000 thread 140318330976000 run wall time: 20.7421
2499999750000000 thread 140318330976000 run cpu time: 0.740057
 main run wall time: 20.7425
main run cpu time: 3.69665
return main

```

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值