objective-c – 如何衡量iOS线程的实际CPU时间?

前端之家收集整理的这篇文章主要介绍了objective-c – 如何衡量iOS线程的实际CPU时间?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找适用于 Android SystemClock.currentThreadTimeMillis()或Microsoft GetThreadTimes()或Posix clock_gettime(CLOCK_THREAD_CPUTIME_ID,)pthread_getcpuclockid()功能的iOS模拟,以测量多线程应用程序中函数使用的实际“干净”时间.也就是说,我不想测量功能中花费的实际挂钟时间,而是测量cpu时间.

我发现有关此here on stackoverflowelsewhere的有趣讨论.不幸的是,它们都不适用于iOS.

在iOS上有这样的功能吗?

解决方法

万一有人在寻找一个好的答案:

不久之前,我在this answer中找到了一些很棒的代码(用于在OSX中查找cpu时间/内存使用情况),并稍微调整了一下.我用它来对ARM上的一些NEON优化进行基准测试.您可能只需要为当前线程获取时间的部分.

  1. #include <sys/types.h>
  2. #include <sys/sysctl.h>
  3. #include <mach/mach_init.h>
  4. #include <mach/mach_host.h>
  5. #include <mach/mach_port.h>
  6. #include <mach/mach_traps.h>
  7. #include <mach/task_info.h>
  8. #include <mach/thread_info.h>
  9. #include <mach/thread_act.h>
  10. #include <mach/vm_region.h>
  11. #include <mach/vm_map.h>
  12. #include <mach/task.h>
  13.  
  14.  
  15. typedef struct {
  16. double utime,stime;
  17. } cpuTime;
  18.  
  19. int get_cpu_time(cpuTime *rpd,bool_t thread_only)
  20. {
  21. task_t task;
  22. kern_return_t error;
  23. mach_msg_type_number_t count;
  24. thread_array_t thread_table;
  25. thread_basic_info_t thi;
  26. thread_basic_info_data_t thi_data;
  27. unsigned table_size;
  28. struct task_basic_info ti;
  29.  
  30. if (thread_only) {
  31. // just get time of this thread
  32. count = THREAD_BASIC_INFO_COUNT;
  33. thi = &thi_data;
  34. error = thread_info(mach_thread_self(),THREAD_BASIC_INFO,(thread_info_t)thi,&count);
  35. rpd->utime = thi->user_time.seconds + thi->user_time.microseconds * 1e-6;
  36. rpd->stime = thi->system_time.seconds + thi->system_time.microseconds * 1e-6;
  37. return 0;
  38. }
  39.  
  40.  
  41. // get total time of the current process
  42.  
  43. task = mach_task_self();
  44. count = TASK_BASIC_INFO_COUNT;
  45. error = task_info(task,TASK_BASIC_INFO,(task_info_t)&ti,&count);
  46. assert(error == KERN_SUCCESS);
  47. { /* calculate cpu times,adapted from top/libtop.c */
  48. unsigned i;
  49. // the following times are for threads which have already terminated and gone away
  50. rpd->utime = ti.user_time.seconds + ti.user_time.microseconds * 1e-6;
  51. rpd->stime = ti.system_time.seconds + ti.system_time.microseconds * 1e-6;
  52. error = task_threads(task,&thread_table,&table_size);
  53. assert(error == KERN_SUCCESS);
  54. thi = &thi_data;
  55. // for each active thread,add up thread time
  56. for (i = 0; i != table_size; ++i) {
  57. count = THREAD_BASIC_INFO_COUNT;
  58. error = thread_info(thread_table[i],&count);
  59. assert(error == KERN_SUCCESS);
  60. if ((thi->flags & TH_FLAGS_IDLE) == 0) {
  61. rpd->utime += thi->user_time.seconds + thi->user_time.microseconds * 1e-6;
  62. rpd->stime += thi->system_time.seconds + thi->system_time.microseconds * 1e-6;
  63. }
  64. error = mach_port_deallocate(mach_task_self(),thread_table[i]);
  65. assert(error == KERN_SUCCESS);
  66. }
  67. error = vm_deallocate(mach_task_self(),(vm_offset_t)thread_table,table_size * sizeof(thread_array_t));
  68. assert(error == KERN_SUCCESS);
  69. }
  70. if (task != mach_task_self()) {
  71. mach_port_deallocate(mach_task_self(),task);
  72. assert(error == KERN_SUCCESS);
  73. }
  74. return 0;
  75. }

猜你在找的C&C++相关文章