关于使用线程的排序速度





关于使用线程的排序速度

关于使用线程的排序速度

线程


用插入排序验证线程进行随机和最坏排序, 以及非线程的随机和最坏排序的时间差距

  
  
  1. /*************************************************************************
  2. > File Name: t.cpp
  3. > Author: Function_Dou
  4. > Mail:
  5. > Created Time: 2018年02月07日 星期三 18时51分14秒
  6. ************************************************************************/
  7. #include <stdio.h>
  8. #include <pthread.h>
  9. #include "apue.h"
  10. #include <sys/time.h>
  11. const int Max = 30000;
  12. const int max = Max - 1;
  13. long arr[Max];
  14. long arr1[Max];
  15. void Sort(long *arr, int num)
  16. {
  17. int i, j, t;
  18. for (i = 0; i < max; i++)
  19. for(j = i + 1; j < Max; j++)
  20. if(num == 0)
  21. {
  22. if(arr[i] < arr[j])
  23. {
  24. t = arr[i];
  25. arr[i] = arr[j];
  26. arr[j] = t;
  27. }
  28. }
  29. else
  30. if(arr[i] > arr[j])
  31. {
  32. t = arr[i];
  33. arr[i] = arr[j];
  34. arr[j] = t;
  35. }
  36. }
  37. void *SortThread(void *)
  38. {
  39. Sort(arr, 0);
  40. pthread_exit((void *)1);
  41. }
  42. void *SortThread2(void *)
  43. {
  44. Sort(arr1, 1);
  45. pthread_exit((void *)1);
  46. }
  47. int main(void)
  48. {
  49. int i;
  50. struct timeval start, end;
  51. // 线程开始的计时
  52. gettimeofday(&start, NULL);
  53. // 生成大量随机数, 用于排序
  54. srandom(1);
  55. for(i = 0; i < Max; i++)
  56. {
  57. arr[i] = random();
  58. arr1[i] = i;
  59. }
  60. // 一个进行随机排序. 一个最坏情况的排序
  61. pthread_t threadid1, threadid2;
  62. pthread_create(&threadid1, NULL, SortThread, NULL);
  63. pthread_create(&threadid2, NULL, SortThread, NULL);
  64. // 线程的结束时间
  65. gettimeofday(&end, NULL);
  66. // 时间的差值计算
  67. printf("pthread no time is : %.4f\n", (double)(end.tv_sec * 1000000 + end.tv_usec - (start.tv_sec * 1000000 - start.tv_usec)) / 1000000);
  68. // ------------------------------------
  69. // 没有线程的排序计时
  70. gettimeofday(&start, NULL);
  71. for(i = 0; i < Max; i++)
  72. arr[i] = random();
  73. // 一个进行随机排序. 一个最坏情况的排序
  74. Sort(arr, 0);
  75. Sort(arr1, 1);
  76. // 线程的结束时间
  77. gettimeofday(&end, NULL);
  78. // 时间的差值计算
  79. printf("pthread no time is : %.4f\n", (double)(end.tv_sec * 1000000 + end.tv_usec - (start.tv_sec * 1000000 - start.tv_usec)) / 1000000);
  80. exit(0);
  81. }

几次运行结果 :

  
  
  1. [root@localhost Pthread]# ./a.out
  2. pthread no time is : 2.0003
  3. pthread no time is : 3.7875
  4. [root@localhost Pthread]# ./a.out
  5. pthread no time is : 0.4818
  6. pthread no time is : 4.2071
  7. [root@localhost Pthread]# ./a.out
  8. pthread no time is : 1.4883
  9. pthread no time is : 5.2815
  10. [root@localhost Pthread]# ./a.out
  11. pthread no time is : 0.9232
  12. pthread no time is : 4.6768

当我将 Sort(arr1, 1) 注释掉后, 运行的结果, 有线程的结果偏小可能是我电脑关掉了些不必要的东西造成的吧. 可以看出, 无线程的排序变化并不大

  
  
  1. [root@localhost Pthread]# ./a.out
  2. pthread no time is : 0.7061
  3. pthread no time is : 3.5253
  4. [root@localhost Pthread]# ./a.out
  5. pthread no time is : 0.4698
  6. pthread no time is : 3.4451
  7. [root@localhost Pthread]# ./a.out
  8. pthread no time is : 1.8373
  9. pthread no time is : 4.7519
  10. [root@localhost Pthread]# ./a.out
  11. pthread no time is : 0.6393
  12. pthread no time is : 3.3885


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值