关于使用线程的排序速度
2018-03-07 21:01
239 查看
关于使用线程的排序速度
用插入排序验证线程进行随机和最坏排序, 以及非线程的随机和最坏排序的时间差距
[/code]几次运行结果 :
[/code]当我将 Sort(arr1, 1) 注释掉后, 运行的结果, 有线程的结果偏小可能是我电脑关掉了些不必要的东西造成的吧. 可以看出, 无线程的排序变化并不大
[/code]
关于使用线程的排序速度
线程
用插入排序验证线程进行随机和最坏排序, 以及非线程的随机和最坏排序的时间差距
/*************************************************************************
> File Name: t.cpp
> Author: Function_Dou
> Mail:
> Created Time: 2018年02月07日 星期三 18时51分14秒
************************************************************************/
#include <stdio.h>
#include <pthread.h>
#include "apue.h"
#include <sys/time.h>
const int Max = 30000;
const int max = Max - 1;
long arr[Max];
long arr1[Max];
void Sort(long *arr, int num)
{
int i, j, t;
for (i = 0; i < max; i++)
for(j = i + 1; j < Max; j++)
if(num == 0)
{
if(arr[i] < arr[j])
{
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
else
if(arr[i] > arr[j])
{
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
void *SortThread(void *)
{
Sort(arr, 0);
pthread_exit((void *)1);
}
void *SortThread2(void *)
{
Sort(arr1, 1);
pthread_exit((void *)1);
}
int main(void)
{
int i;
struct timeval start, end;
// 线程开始的计时
gettimeofday(&start, NULL);
// 生成大量随机数, 用于排序
srandom(1);
for(i = 0; i < Max; i++)
{
arr[i] = random();
arr1[i] = i;
}
// 一个进行随机排序. 一个最坏情况的排序
pthread_t threadid1, threadid2;
pthread_create(&threadid1, NULL, SortThread, NULL);
pthread_create(&threadid2, NULL, SortThread, NULL);
// 线程的结束时间
gettimeofday(&end, NULL);
// 时间的差值计算
printf("pthread no time is : %.4f\n", (double)(end.tv_sec * 1000000 + end.tv_usec - (start.tv_sec * 1000000 - start.tv_usec)) / 1000000);
// ------------------------------------
// 没有线程的排序计时
gettimeofday(&start, NULL);
for(i = 0; i < Max; i++)
arr[i] = random();
// 一个进行随机排序. 一个最坏情况的排序
Sort(arr, 0);
Sort(arr1, 1);
// 线程的结束时间
gettimeofday(&end, NULL);
// 时间的差值计算
printf("pthread no time is : %.4f\n", (double)(end.tv_sec * 1000000 + end.tv_usec - (start.tv_sec * 1000000 - start.tv_usec)) / 1000000);
exit(0);
}
[/code]几次运行结果 :
[root@localhost Pthread]# ./a.out
pthread no time is : 2.0003
pthread no time is : 3.7875
[root@localhost Pthread]# ./a.out
pthread no time is : 0.4818
pthread no time is : 4.2071
[root@localhost Pthread]# ./a.out
pthread no time is : 1.4883
pthread no time is : 5.2815
[root@localhost Pthread]# ./a.out
pthread no time is : 0.9232
pthread no time is : 4.6768
[/code]当我将 Sort(arr1, 1) 注释掉后, 运行的结果, 有线程的结果偏小可能是我电脑关掉了些不必要的东西造成的吧. 可以看出, 无线程的排序变化并不大
[root@localhost Pthread]# ./a.out
pthread no time is : 0.7061
pthread no time is : 3.5253
[root@localhost Pthread]# ./a.out
pthread no time is : 0.4698
pthread no time is : 3.4451
[root@localhost Pthread]# ./a.out
pthread no time is : 1.8373
pthread no time is : 4.7519
[root@localhost Pthread]# ./a.out
pthread no time is : 0.6393
pthread no time is : 3.3885
[/code]
相关文章推荐
- 关于在线程中使用AfxGetMainWnd()出错的问题
- 关于TLIST排序SORT的使用
- 关于C#线程,线程池和并行运算的简单使用和对比
- 线程(两种排序方法速度的比较)
- Android中关于线程使用的几点注意事项
- php关于array_multisort多维数组排序的使用说明
- 关于在c++的类中使用线程的问题
- 一起谈.NET技术,关于C#线程,线程池和并行运算的简单使用和对比
- 关于线程的使用
- Android中关于线程使用的几点注意事项
- 关于C#Winform线程调用窗体的使用方法以及窗体的单一显示
- 关于排序rank () over , row_number() over ,rank_dense ()函数使用
- Android学习笔记(10)---关于线程Thread的使用
- 从 VCL 中吸取营养--使用多窗体时, 关于节约内存和加快启动速度的思考与尝试
- 关于定时器和线程的使用
- 关于使用Comparator对集合进行强制排序.
- 使用多窗体时, 关于节约内存和加快启动速度的思考与尝试
- 关于在线程中使用AfxGetMainWnd()出错的问题,终于找到了
- 关于在线程中使用AfxGetMainWnd()出错的问题,终于找到了
- 关于C#线程,线程池和并行运算的简单使用和对比