computing by multi thread

最近在看一本叫做《Computer Systems-A Programmer’s Perspective》Second Edition, 中文名叫《深入理解计算机系统》, 觉得这个本讲得还是很不错的,里面的例子对于理解书中所讲的知识点很有帮助,比如在讲多线程中,列举了用多个线程来计算一系统数据来展示多线程的优点。自己跟着书中的 demo 动手实现了一下。书中用最原始的 pthread 来编程,代码如下:

#include <iostream>
#include <pthread.h>
#include <cstdio>
#include<climits>
#include <mutex>
#include <time.h>
#include <iomanip>

using namespace std;

#define MAXTHREADS 32
std::mutex mtx;

double getElapsedTime(clock_t begin,clock_t end)
{
	//marginof error(tolerance): 0-0.01ms
	double ticks = end - begin;;
	return (ticks*10)/CLOCKS_PER_SEC*1.0;
}

void *sum(void *vargp);

/* Global shared variables */
unsigned long long psum[MAXTHREADS];  /* Partial sum computed by each thread */
double timec[MAXTHREADS]={0}; /* Partial sum computed by each thread */
unsigned long long nelems_per_thread;  /* Number of elements sumed by each thread */
unsigned long left_eleme;
long nthreads;
int main(int argc, char *argv[])
{

	int count=20;
	//	while(count) {
	//	cout<<"max unsgined long long :"<<ULLONG_MAX<<endl;
	unsigned long long  i,long_nelems=0;
	unsigned long long result=0;
	pthread_t tid[MAXTHREADS];
	int threadid[MAXTHREADS];
	/* Get inpu arguments
	   if (argc != 3)
	   {
	   printf("%s<nthreads> <long_nelems>\n",argv[0]);
	   exit(0);
	   } */
	left_eleme = 0;
	//nthreads= 2;
	//nelems=10;
	nthreads= atoi(argv[1]);
	//long_nelems=atoi(argv[2]);
	//	nthreads = count;
	//long_nelems=30;
	unsigned long long nelems=UINT_MAX;
	//unsigned long long nelems=ULLONG_MAX;
	//nelems=(1L << long_nelems);
	nelems_per_thread = nelems / nthreads;
	//printf("%d 2's bit\n",long_nelems);
	if (nelems % nthreads != 0)
	{
		left_eleme = nelems %nthreads;
		cout<<"left number "<<left_eleme<<endl;
		nthreads++;
	}

	/* Create peer threads and wait fro them to finish */
	for (i = 0; i < nthreads; ++i)
	{
		threadid[i] = i;
		pthread_create(&tid[i],NULL,sum,&threadid[i]);
	}

	/* main thread wait for all child thread to exit */
	for (i = 0; i < nthreads; ++i)
	{
		pthread_join(tid[i],NULL);
	}

	/* Add up the partial sume computed by each thread */
	double totalTimeC=0;
	mtx.lock();
	cout<<"max number "<<nelems;
	cout<<" | "<<nthreads<< " thread counts| ";
	cout<<"average number "<<nelems_per_thread<<endl;
	cout<<"time consumption details: ms "<<endl;
	for (i = 0; i < nthreads; ++i)
	{
		totalTimeC +=timec[i];
		cout<<timec[i]<<"\t";
		result += psum[i];
	}
	printf("\n---------------------------------------------\n");
	printf("total time  consumed by all thread:%f ms\n", totalTimeC);

	/* Check final answer */
	if (result != nelems * (nelems +1 )/2)
	{
		cout<<" Error: result "<<result<<endl;
		exit(1);
	}
	else
	{
		//		printf("done: result =%ld\n", result);
	}
	printf("===========================================================\n");
	//		count--;
	mtx.unlock();
	//	}
	exit(0);
	return 0;
}
void *sum(void *vargp)
{
	clock_t beginc = clock();
	int id = *((int *)vargp);
	long long start = id * nelems_per_thread +1;  // 0:1  1:6
	long long end = 0;
	if (left_eleme !=0 && id == nthreads-1)
	{
		end= (nthreads -1 ) * nelems_per_thread + left_eleme;
	}
	else
	{
		end= (id+1) * nelems_per_thread;
	}
	long long  i=0;
	long long sum =0;
	for (i = start; i <= end; ++i)
	{
		sum += i;
	}
	psum[id] = sum;  /* Global psum is unique */
	clock_t endc = clock();
	double time1=getElapsedTime(beginc,endc);
	timec[id]=time1;
	return NULL;
}

自己用 cpp 里面的 thread 类改了一下运行效果几乎一样,不过感觉还是 cpp 的 thread 简洁一些,代码如下:

#include <iostream>
#include <cstdio>
#include <climits>
#include <thread>
#include <vector>
#include <mutex>
#include <time.h>
#include <iomanip>

using namespace std;
#define MAXTHREADS 32
std::mutex mtx;

void sum(int vargp);
double getElapsedTime(clock_t begin,clock_t end)
{
	//marginof error(tolerance): 0-0.01ms
	double ticks = end - begin;;
	return (ticks*10)/CLOCKS_PER_SEC*1.0;
}

/* Global shared variables */
std::vector<unsigned long long> pvsum;  /* Partial sum computed by each thread */
std::vector<double> timevc; /* Partial sum computed by each thread */
unsigned long long nelems_per_thread;  /* Number of elements sumed by each thread */
unsigned long left_eleme;
long nthreads;
int main(int argc, char *argv[])
{

	int count=20;
	while(count)
	{
		unsigned long long result=0;
		/* Get inpu arguments
		   if (argc != 3)
		   {
		   printf("%s<nthreads> <long_nelems>\n",argv[0]);
		   exit(0);
		   } */
		left_eleme = 0;
		//nthreads= 5;
		//	nthreads= atoi(argv[1]);
		//long_nelems=atoi(argv[2]);
		nthreads = count;
		unsigned long long nelems=UINT_MAX;
		//unsigned long long nelems=ULLONG_MAX;
		//	cout<<"max unsgined long long :"<<ULLONG_MAX<<endl;
		nelems_per_thread = nelems / nthreads;
		if (nelems % nthreads != 0)
		{
			left_eleme = nelems %nthreads;
			//cout<<"left number "<<left_eleme<<" add 1 more thread"<<endl;
			nthreads++;
		}

		/* Create peer threads and wait fro them to finish */
		std::vector<std::thread> threadSet;
		for (int i = 0; i < nthreads; ++i)
		{
			try{
				threadSet.push_back(std::thread(sum,i));
				//threadSet.push_back(std::thread(sum,&i));
				//}catch(runtime_error e){
		}catch(system_error e){
			//}catch(...){
			//cout<<"error occurs!\n";
		cout<<e.what()<<endl;
		cout<<e.code()<<endl;
		}
		}

		for (auto &t : threadSet) {
			//t.detach();
			t.join();
		}
		while(!threadSet.empty()) {
			threadSet.pop_back();
		}

		/* Add up the partial sume computed by each thread */
		double totalTimeC=0;
		printf("---------------------------------------------------------------------------------\n");
		cout<<"max number "<<nelems;
		cout<<" | "<<nthreads<< " thread counts| ";
		cout<<"average number "<<nelems_per_thread<<endl;
		cout<<"timec count:"<<timevc.size()<<endl;
		cout<<"pvsum count:"<<pvsum.size()<<endl;
		cout<<"time consumption details: ms: "<<endl;
		for (auto i : timevc) {
			cout<<i<<" ";
			totalTimeC += i;
		}
		for (auto i : pvsum) {
			result +=i;
		}
		cout<<"\ntotal time(compute resource)  consumed by all "<<nthreads<<" thread: "<<totalTimeC<<" ms\n";
		/* Check final answer */
		if (result != nelems * (nelems +1 )/2)
		{
			cout<<" Error: result "<<result<<endl;
			exit(1);
		}
		else
		{
		}
		printf("=======================================================================================\n");
		timevc.clear();
		pvsum.clear();
		count--;
	}
	exit(0);
	return 0;
}

void sum(int vargp)
{
	clock_t beginc = clock();
	int id = vargp;
	long long start = id * nelems_per_thread +1;  // 0:1  1:6
	long long end = 0;
	if (left_eleme !=0 && id == nthreads-1)
	{
		end= (nthreads -1 ) * nelems_per_thread + left_eleme;
	}
	else
	{
		end= (id+1) * nelems_per_thread;
	}
	long long  i=0;
	long long sum =0;
	for (i = start; i <= end; ++i)
	{
		sum += i;
	}
	clock_t endc = clock();
	mtx.lock();   /* add lock or output dislay may be in a mess */
	pvsum.push_back(sum);
	double time1=getElapsedTime(beginc,endc);
	timevc.push_back(time1);
	cout<<"compute start at "<<start<<", end at "<<end<<" by thread id "<<std::this_thread::get_id()<<" thread run id "<<id<<" time clock use:"<<time1<<endl;
	mtx.unlock();
}

运行的结果如下:

./MainExecution
compute start at 4294967281, end at 4294967295 by thread id 0x10250b000 thread run id 20 time clock use:3e-05
compute start at 3865470553, end at 4080218916 by thread id 0x102405000 thread run id 18 time clock use:210.04
compute start at 3435973825, end at 3650722188 by thread id 0x1022ff000 thread run id 16 time clock use:210.345
compute start at 1, end at 214748364 by thread id 0x101acf000 thread run id 0 time clock use:212.157
compute start at 644245093, end at 858993456 by thread id 0x101c58000 thread run id 3 time clock use:212.376
compute start at 214748365, end at 429496728 by thread id 0x101b52000 thread run id 1 time clock use:212.882
compute start at 2791728733, end at 3006477096 by thread id 0x102176000 thread run id 13 time clock use:212.436
compute start at 2362232005, end at 2576980368 by thread id 0x102070000 thread run id 11 time clock use:212.939
compute start at 2147483641, end at 2362232004 by thread id 0x101fed000 thread run id 10 time clock use:213.158
compute start at 1073741821, end at 1288490184 by thread id 0x101d5e000 thread run id 5 time clock use:213.694
compute start at 2576980369, end at 2791728732 by thread id 0x1020f3000 thread run id 12 time clock use:213.322
compute start at 4080218917, end at 4294967280 by thread id 0x102488000 thread run id 19 time clock use:213.321
compute start at 3650722189, end at 3865470552 by thread id 0x102382000 thread run id 17 time clock use:213.525
compute start at 429496729, end at 644245092 by thread id 0x101bd5000 thread run id 2 time clock use:215.314
compute start at 858993457, end at 1073741820 by thread id 0x101cdb000 thread run id 4 time clock use:215.352
compute start at 3006477097, end at 3221225460 by thread id 0x1021f9000 thread run id 14 time clock use:214.26
compute start at 1503238549, end at 1717986912 by thread id 0x101e64000 thread run id 7 time clock use:215.413
compute start at 1717986913, end at 1932735276 by thread id 0x101ee7000 thread run id 8 time clock use:215.225
compute start at 3221225461, end at 3435973824 by thread id 0x10227c000 thread run id 15 time clock use:214.593
compute start at 1932735277, end at 2147483640 by thread id 0x101f6a000 thread run id 9 time clock use:215.54
compute start at 1288490185, end at 1503238548 by thread id 0x101de1000 thread run id 6 time clock use:215.85
---------------------------------------------------------------------------------
max number 4294967295 | 21 thread counts| average number 214748364
time consumption details: ms:
212.157	212.882	215.314	212.376	215.352	213.694	215.85	215.413	215.225	215.54	213.158	212.939	213.322	212.436	214.26	214.593	210.345	213.525	210.04	213.321	3e-05
total time  consumed by all 21 thread: 4271.74 ms
=======================================================================================
compute start at 4294967291, end at 4294967295 by thread id 0x102488000 thread run id 19 time clock use:4e-05
compute start at 2938661831, end at 3164712740 by thread id 0x102176000 thread run id 13 time clock use:209.01
compute start at 1582356371, end at 1808407280 by thread id 0x101e64000 thread run id 7 time clock use:212.327
compute start at 2260509101, end at 2486560010 by thread id 0x101fed000 thread run id 10 time clock use:213.128
compute start at 3390763651, end at 3616814560 by thread id 0x10227c000 thread run id 15 time clock use:213.226
compute start at 1, end at 226050910 by thread id 0x101acf000 thread run id 0 time clock use:214.728
compute start at 452101821, end at 678152730 by thread id 0x101bd5000 thread run id 2 time clock use:214.89
compute start at 2034458191, end at 2260509100 by thread id 0x101f6a000 thread run id 9 time clock use:214.969
compute start at 3616814561, end at 3842865470 by thread id 0x1022ff000 thread run id 16 time clock use:214.196
compute start at 3842865471, end at 4068916380 by thread id 0x102382000 thread run id 17 time clock use:214.018
compute start at 2486560011, end at 2712610920 by thread id 0x102070000 thread run id 11 time clock use:214.956
compute start at 3164712741, end at 3390763650 by thread id 0x1021f9000 thread run id 14 time clock use:214.391
compute start at 2712610921, end at 2938661830 by thread id 0x1020f3000 thread run id 12 time clock use:214.897
compute start at 226050911, end at 452101820 by thread id 0x101b52000 thread run id 1 time clock use:215.688
compute start at 1808407281, end at 2034458190 by thread id 0x101ee7000 thread run id 8 time clock use:215.529
compute start at 678152731, end at 904203640 by thread id 0x101c58000 thread run id 3 time clock use:215.918
compute start at 1356305461, end at 1582356370 by thread id 0x101de1000 thread run id 6 time clock use:215.865
compute start at 1130254551, end at 1356305460 by thread id 0x101d5e000 thread run id 5 time clock use:216.087
compute start at 4068916381, end at 4294967290 by thread id 0x102405000 thread run id 18 time clock use:214.735
compute start at 904203641, end at 1130254550 by thread id 0x101cdb000 thread run id 4 time clock use:216.257
---------------------------------------------------------------------------------
max number 4294967295 | 20 thread counts| average number 226050910
time consumption details: ms:
214.728	215.688	214.89	215.918	216.257	216.087	215.865	212.327	215.529	214.969	213.128	214.956	214.897	209.01	214.391	213.226	214.196	214.018	214.735	4e-05
total time  consumed by all 20 thread: 4074.81 ms
=======================================================================================
compute start at 4294967293, end at 4294967295 by thread id 0x102405000 thread run id 18 time clock use:3e-05
compute start at 477218589, end at 715827882 by thread id 0x101bd5000 thread run id 2 time clock use:206.18
compute start at 1670265059, end at 1908874352 by thread id 0x101e64000 thread run id 7 time clock use:207.389
compute start at 1193046471, end at 1431655764 by thread id 0x101d5e000 thread run id 5 time clock use:208.534
compute start at 238609295, end at 477218588 by thread id 0x101b52000 thread run id 1 time clock use:213.006
compute start at 1431655765, end at 1670265058 by thread id 0x101de1000 thread run id 6 time clock use:212.962
compute start at 3101920823, end at 3340530116 by thread id 0x102176000 thread run id 13 time clock use:212.365
compute start at 1, end at 238609294 by thread id 0x101acf000 thread run id 0 time clock use:213.618
compute start at 2863311529, end at 3101920822 by thread id 0x1020f3000 thread run id 12 time clock use:212.881
compute start at 1908874353, end at 2147483646 by thread id 0x101ee7000 thread run id 8 time clock use:214.301
compute start at 2624702235, end at 2863311528 by thread id 0x102070000 thread run id 11 time clock use:214.142
compute start at 3340530117, end at 3579139410 by thread id 0x1021f9000 thread run id 14 time clock use:213.977
compute start at 954437177, end at 1193046470 by thread id 0x101cdb000 thread run id 4 time clock use:215.134
compute start at 2386092941, end at 2624702234 by thread id 0x101fed000 thread run id 10 time clock use:214.986
compute start at 3579139411, end at 3817748704 by thread id 0x10227c000 thread run id 15 time clock use:214.533
compute start at 2147483647, end at 2386092940 by thread id 0x101f6a000 thread run id 9 time clock use:215.375
compute start at 715827883, end at 954437176 by thread id 0x101c58000 thread run id 3 time clock use:216.13
compute start at 3817748705, end at 4056357998 by thread id 0x1022ff000 thread run id 16 time clock use:215.059
compute start at 4056357999, end at 4294967292 by thread id 0x102382000 thread run id 17 time clock use:214.855
---------------------------------------------------------------------------------
max number 4294967295 | 19 thread counts| average number 238609294
time consumption details: ms:
213.618	213.006	206.18	216.13	215.134	208.534	212.962	207.389	214.301	215.375	214.986	214.142	212.881	212.365	213.977	214.533	215.059	214.855	3e-05
total time  consumed by all 19 thread: 3835.43 ms
=======================================================================================
compute start at 3284386756, end at 3537031890 by thread id 0x102176000 thread run id 13 time clock use:204.218
compute start at 505290271, end at 757935405 by thread id 0x101bd5000 thread run id 2 time clock use:209.939
compute start at 1768515946, end at 2021161080 by thread id 0x101e64000 thread run id 7 time clock use:210.735
compute start at 4042322161, end at 4294967295 by thread id 0x1022ff000 thread run id 16 time clock use:210.586
compute start at 2779096486, end at 3031741620 by thread id 0x102070000 thread run id 11 time clock use:211.918
compute start at 3789677026, end at 4042322160 by thread id 0x10227c000 thread run id 15 time clock use:211.96
compute start at 1263225676, end at 1515870810 by thread id 0x101d5e000 thread run id 5 time clock use:213.607
compute start at 757935406, end at 1010580540 by thread id 0x101c58000 thread run id 3 time clock use:214.019
compute start at 1010580541, end at 1263225675 by thread id 0x101cdb000 thread run id 4 time clock use:214.143
compute start at 1, end at 252645135 by thread id 0x101acf000 thread run id 0 time clock use:214.526
compute start at 3537031891, end at 3789677025 by thread id 0x1021f9000 thread run id 14 time clock use:213.865
compute start at 252645136, end at 505290270 by thread id 0x101b52000 thread run id 1 time clock use:215.116
compute start at 2273806216, end at 2526451350 by thread id 0x101f6a000 thread run id 9 time clock use:214.533
compute start at 3031741621, end at 3284386755 by thread id 0x1020f3000 thread run id 12 time clock use:214.556
compute start at 1515870811, end at 1768515945 by thread id 0x101de1000 thread run id 6 time clock use:215.255
compute start at 2021161081, end at 2273806215 by thread id 0x101ee7000 thread run id 8 time clock use:215.246
compute start at 2526451351, end at 2779096485 by thread id 0x101fed000 thread run id 10 time clock use:215.086
---------------------------------------------------------------------------------
max number 4294967295 | 17 thread counts| average number 252645135
time consumption details: ms:
214.526	215.116	209.939	214.019	214.143	213.607	215.255	210.735	215.246	214.533	215.086	211.918	214.556	204.218	213.865	211.96	210.586
total time  consumed by all 17 thread: 3619.31 ms
=======================================================================================
compute start at 4294967281, end at 4294967295 by thread id 0x1022ff000 thread run id 16 time clock use:2e-05
compute start at 805306366, end at 1073741820 by thread id 0x101c58000 thread run id 3 time clock use:204.084
compute start at 4026531826, end at 4294967280 by thread id 0x10227c000 thread run id 15 time clock use:208.245
compute start at 2415919096, end at 2684354550 by thread id 0x101f6a000 thread run id 9 time clock use:209.762
compute start at 536870911, end at 805306365 by thread id 0x101bd5000 thread run id 2 time clock use:211.836
compute start at 1879048186, end at 2147483640 by thread id 0x101e64000 thread run id 7 time clock use:211.431
compute start at 1342177276, end at 1610612730 by thread id 0x101d5e000 thread run id 5 time clock use:213.484
compute start at 2147483641, end at 2415919095 by thread id 0x101ee7000 thread run id 8 time clock use:214.81
compute start at 1073741821, end at 1342177275 by thread id 0x101cdb000 thread run id 4 time clock use:216.02
compute start at 1610612731, end at 1879048185 by thread id 0x101de1000 thread run id 6 time clock use:216.178
compute start at 2952790006, end at 3221225460 by thread id 0x102070000 thread run id 11 time clock use:215.662
compute start at 2684354551, end at 2952790005 by thread id 0x101fed000 thread run id 10 time clock use:215.911
compute start at 3489660916, end at 3758096370 by thread id 0x102176000 thread run id 13 time clock use:215.609
compute start at 3758096371, end at 4026531825 by thread id 0x1021f9000 thread run id 14 time clock use:215.666
compute start at 3221225461, end at 3489660915 by thread id 0x1020f3000 thread run id 12 time clock use:215.936
compute start at 1, end at 268435455 by thread id 0x101acf000 thread run id 0 time clock use:217.051
compute start at 268435456, end at 536870910 by thread id 0x101b52000 thread run id 1 time clock use:217.071
---------------------------------------------------------------------------------
max number 4294967295 | 17 thread counts| average number 268435455
time consumption details: ms:
217.051	217.071	211.836	204.084	216.02	213.484	216.178	211.431	214.81	209.762	215.911	215.662	215.936	215.609	215.666	208.245	2e-05
total time  consumed by all 17 thread: 3418.76 ms
=======================================================================================
compute start at 2863311531, end at 3149642683 by thread id 0x101fed000 thread run id 10 time clock use:208.158
compute start at 1717986919, end at 2004318071 by thread id 0x101de1000 thread run id 6 time clock use:210.576
compute start at 1145324613, end at 1431655765 by thread id 0x101cdb000 thread run id 4 time clock use:213.055
compute start at 286331154, end at 572662306 by thread id 0x101b52000 thread run id 1 time clock use:213.505
compute start at 572662307, end at 858993459 by thread id 0x101bd5000 thread run id 2 time clock use:213.916
compute start at 2004318072, end at 2290649224 by thread id 0x101e64000 thread run id 7 time clock use:213.439
compute start at 858993460, end at 1145324612 by thread id 0x101c58000 thread run id 3 time clock use:213.894
compute start at 3722304990, end at 4008636142 by thread id 0x102176000 thread run id 13 time clock use:213.872
compute start at 2290649225, end at 2576980377 by thread id 0x101ee7000 thread run id 8 time clock use:215.041
compute start at 1431655766, end at 1717986918 by thread id 0x101d5e000 thread run id 5 time clock use:215.486
compute start at 1, end at 286331153 by thread id 0x101acf000 thread run id 0 time clock use:215.875
compute start at 3149642684, end at 3435973836 by thread id 0x102070000 thread run id 11 time clock use:215.131
compute start at 3435973837, end at 3722304989 by thread id 0x1020f3000 thread run id 12 time clock use:215.004
compute start at 4008636143, end at 4294967295 by thread id 0x1021f9000 thread run id 14 time clock use:215.011
compute start at 2576980378, end at 2863311530 by thread id 0x101f6a000 thread run id 9 time clock use:215.688
---------------------------------------------------------------------------------
max number 4294967295 | 15 thread counts| average number 286331153
time consumption details: ms:
215.875	213.505	213.916	213.894	213.055	215.486	210.576	213.439	215.041	215.688	208.158	215.131	215.004	213.872	215.011
total time  consumed by all 15 thread: 3207.65 ms
=======================================================================================
compute start at 4294967293, end at 4294967295 by thread id 0x1021f9000 thread run id 14 time clock use:0.00028
compute start at 2147483647, end at 2454267024 by thread id 0x101e64000 thread run id 7 time clock use:206.958
compute start at 3681400537, end at 3988183914 by thread id 0x1020f3000 thread run id 12 time clock use:210.52
compute start at 306783379, end at 613566756 by thread id 0x101b52000 thread run id 1 time clock use:212.632
compute start at 920350135, end at 1227133512 by thread id 0x101c58000 thread run id 3 time clock use:214.358
compute start at 1840700269, end at 2147483646 by thread id 0x101de1000 thread run id 6 time clock use:214.611
compute start at 1533916891, end at 1840700268 by thread id 0x101d5e000 thread run id 5 time clock use:214.877
compute start at 3988183915, end at 4294967292 by thread id 0x102176000 thread run id 13 time clock use:214.248
compute start at 2761050403, end at 3067833780 by thread id 0x101f6a000 thread run id 9 time clock use:214.863
compute start at 1, end at 306783378 by thread id 0x101acf000 thread run id 0 time clock use:215.528
compute start at 2454267025, end at 2761050402 by thread id 0x101ee7000 thread run id 8 time clock use:215.131
compute start at 3374617159, end at 3681400536 by thread id 0x102070000 thread run id 11 time clock use:214.946
compute start at 613566757, end at 920350134 by thread id 0x101bd5000 thread run id 2 time clock use:215.987
compute start at 1227133513, end at 1533916890 by thread id 0x101cdb000 thread run id 4 time clock use:215.823
compute start at 3067833781, end at 3374617158 by thread id 0x101fed000 thread run id 10 time clock use:215.342
---------------------------------------------------------------------------------
max number 4294967295 | 15 thread counts| average number 306783378
time consumption details: ms:
215.528	212.632	215.987	214.358	215.823	214.877	214.611	206.958	215.131	214.863	215.342	214.946	210.52	214.248	0.00028
total time  consumed by all 15 thread: 2995.82 ms
=======================================================================================
compute start at 4294967288, end at 4294967295 by thread id 0x102176000 thread run id 13 time clock use:6e-05
compute start at 2973438892, end at 3303820990 by thread id 0x101f6a000 thread run id 9 time clock use:212.458
compute start at 2312674694, end at 2643056792 by thread id 0x101e64000 thread run id 7 time clock use:213.225
compute start at 991146298, end at 1321528396 by thread id 0x101c58000 thread run id 3 time clock use:214.317
compute start at 1651910496, end at 1982292594 by thread id 0x101d5e000 thread run id 5 time clock use:214.526
compute start at 3634203090, end at 3964585188 by thread id 0x102070000 thread run id 11 time clock use:214.239
compute start at 1982292595, end at 2312674693 by thread id 0x101de1000 thread run id 6 time clock use:215.902
compute start at 1, end at 330382099 by thread id 0x101acf000 thread run id 0 time clock use:216.429
compute start at 660764199, end at 991146297 by thread id 0x101bd5000 thread run id 2 time clock use:216.499
compute start at 330382100, end at 660764198 by thread id 0x101b52000 thread run id 1 time clock use:216.628
compute start at 3303820991, end at 3634203089 by thread id 0x101fed000 thread run id 10 time clock use:215.895
compute start at 2643056793, end at 2973438891 by thread id 0x101ee7000 thread run id 8 time clock use:215.976
compute start at 3964585189, end at 4294967287 by thread id 0x1020f3000 thread run id 12 time clock use:215.77
compute start at 1321528397, end at 1651910495 by thread id 0x101cdb000 thread run id 4 time clock use:216.929
---------------------------------------------------------------------------------
max number 4294967295 | 14 thread counts| average number 330382099
time consumption details: ms:
216.429	216.628	216.499	214.317	216.929	214.526	215.902	213.225	215.976	212.458	215.895	214.239	215.77	6e-05
total time  consumed by all 14 thread: 2798.79 ms
=======================================================================================
compute start at 4294967293, end at 4294967295 by thread id 0x1020f3000 thread run id 12 time clock use:5e-05
compute start at 2147483647, end at 2505397587 by thread id 0x101de1000 thread run id 6 time clock use:213.419
compute start at 357913942, end at 715827882 by thread id 0x101b52000 thread run id 1 time clock use:214.31
compute start at 2863311529, end at 3221225469 by thread id 0x101ee7000 thread run id 8 time clock use:214.018
compute start at 3579139411, end at 3937053351 by thread id 0x101fed000 thread run id 10 time clock use:213.868
compute start at 1073741824, end at 1431655764 by thread id 0x101c58000 thread run id 3 time clock use:214.731
compute start at 3937053352, end at 4294967292 by thread id 0x102070000 thread run id 11 time clock use:214.235
compute start at 1, end at 357913941 by thread id 0x101acf000 thread run id 0 time clock use:215.084
compute start at 1431655765, end at 1789569705 by thread id 0x101cdb000 thread run id 4 time clock use:215.836
compute start at 715827883, end at 1073741823 by thread id 0x101bd5000 thread run id 2 time clock use:216.008
compute start at 1789569706, end at 2147483646 by thread id 0x101d5e000 thread run id 5 time clock use:215.674
compute start at 2505397588, end at 2863311528 by thread id 0x101e64000 thread run id 7 time clock use:215.776
compute start at 3221225470, end at 3579139410 by thread id 0x101f6a000 thread run id 9 time clock use:215.434
---------------------------------------------------------------------------------
max number 4294967295 | 13 thread counts| average number 357913941
time consumption details: ms:
215.084	214.31	216.008	214.731	215.836	215.674	213.419	215.776	214.018	215.434	213.868	214.235	5e-05
total time  consumed by all 13 thread: 2578.39 ms
=======================================================================================
compute start at 4294967293, end at 4294967295 by thread id 0x102070000 thread run id 11 time clock use:2e-05
compute start at 1171354717, end at 1561806288 by thread id 0x101c58000 thread run id 3 time clock use:209.731
compute start at 3123612577, end at 3514064148 by thread id 0x101ee7000 thread run id 8 time clock use:213.309
compute start at 2342709433, end at 2733161004 by thread id 0x101de1000 thread run id 6 time clock use:213.554
compute start at 1561806289, end at 1952257860 by thread id 0x101cdb000 thread run id 4 time clock use:214.615
compute start at 390451573, end at 780903144 by thread id 0x101b52000 thread run id 1 time clock use:215.565
compute start at 3904515721, end at 4294967292 by thread id 0x101fed000 thread run id 10 time clock use:215.317
compute start at 3514064149, end at 3904515720 by thread id 0x101f6a000 thread run id 9 time clock use:215.623
compute start at 2733161005, end at 3123612576 by thread id 0x101e64000 thread run id 7 time clock use:216.407
compute start at 780903145, end at 1171354716 by thread id 0x101bd5000 thread run id 2 time clock use:217.032
compute start at 1952257861, end at 2342709432 by thread id 0x101d5e000 thread run id 5 time clock use:216.769
compute start at 1, end at 390451572 by thread id 0x101acf000 thread run id 0 time clock use:217.078
---------------------------------------------------------------------------------
max number 4294967295 | 12 thread counts| average number 390451572
time consumption details: ms:
217.078	215.565	217.032	209.731	214.615	216.769	213.554	216.407	213.309	215.623	215.317	2e-05
total time  consumed by all 12 thread: 2365 ms
=======================================================================================
compute start at 4294967291, end at 4294967295 by thread id 0x101fed000 thread run id 10 time clock use:9e-05
compute start at 1, end at 429496729 by thread id 0x101acf000 thread run id 0 time clock use:214.44
compute start at 1717986917, end at 2147483645 by thread id 0x101cdb000 thread run id 4 time clock use:214.281
compute start at 858993459, end at 1288490187 by thread id 0x101bd5000 thread run id 2 time clock use:214.743
compute start at 3435973833, end at 3865470561 by thread id 0x101ee7000 thread run id 8 time clock use:214.063
compute start at 2147483646, end at 2576980374 by thread id 0x101d5e000 thread run id 5 time clock use:214.417
compute start at 3865470562, end at 4294967290 by thread id 0x101f6a000 thread run id 9 time clock use:214.123
compute start at 429496730, end at 858993458 by thread id 0x101b52000 thread run id 1 time clock use:215.113
compute start at 3006477104, end at 3435973832 by thread id 0x101e64000 thread run id 7 time clock use:214.503
compute start at 2576980375, end at 3006477103 by thread id 0x101de1000 thread run id 6 time clock use:214.64
compute start at 1288490188, end at 1717986916 by thread id 0x101c58000 thread run id 3 time clock use:215.301
---------------------------------------------------------------------------------
max number 4294967295 | 11 thread counts| average number 429496729
time consumption details: ms:
214.44	215.113	214.743	215.301	214.281	214.417	214.64	214.503	214.063	214.123	9e-05
total time  consumed by all 11 thread: 2145.62 ms
=======================================================================================
compute start at 4294967293, end at 4294967295 by thread id 0x101f6a000 thread run id 9 time clock use:2e-05
compute start at 3817748705, end at 4294967292 by thread id 0x101ee7000 thread run id 8 time clock use:210.677
compute start at 954437177, end at 1431655764 by thread id 0x101bd5000 thread run id 2 time clock use:214.328
compute start at 1908874353, end at 2386092940 by thread id 0x101cdb000 thread run id 4 time clock use:214.875
compute start at 477218589, end at 954437176 by thread id 0x101b52000 thread run id 1 time clock use:215.462
compute start at 1431655765, end at 1908874352 by thread id 0x101c58000 thread run id 3 time clock use:215.71
compute start at 2863311529, end at 3340530116 by thread id 0x101de1000 thread run id 6 time clock use:215.532
compute start at 2386092941, end at 2863311528 by thread id 0x101d5e000 thread run id 5 time clock use:215.681
compute start at 1, end at 477218588 by thread id 0x101acf000 thread run id 0 time clock use:216.334
compute start at 3340530117, end at 3817748704 by thread id 0x101e64000 thread run id 7 time clock use:215.883
---------------------------------------------------------------------------------
max number 4294967295 | 10 thread counts| average number 477218588
time consumption details: ms:
216.334	215.462	214.328	215.71	214.875	215.681	215.532	215.883	210.677	2e-05
total time  consumed by all 10 thread: 1934.48 ms
=======================================================================================
compute start at 4294967289, end at 4294967295 by thread id 0x101ee7000 thread run id 8 time clock use:2e-05
compute start at 2684354556, end at 3221225466 by thread id 0x101d5e000 thread run id 5 time clock use:212.112
compute start at 3758096378, end at 4294967288 by thread id 0x101e64000 thread run id 7 time clock use:213.725
compute start at 1, end at 536870911 by thread id 0x101acf000 thread run id 0 time clock use:214.5
compute start at 2147483645, end at 2684354555 by thread id 0x101cdb000 thread run id 4 time clock use:214.575
compute start at 1073741823, end at 1610612733 by thread id 0x101bd5000 thread run id 2 time clock use:215.062
compute start at 536870912, end at 1073741822 by thread id 0x101b52000 thread run id 1 time clock use:215.489
compute start at 1610612734, end at 2147483644 by thread id 0x101c58000 thread run id 3 time clock use:215.566
compute start at 3221225467, end at 3758096377 by thread id 0x101de1000 thread run id 6 time clock use:215.346
---------------------------------------------------------------------------------
max number 4294967295 | 9 thread counts| average number 536870911
time consumption details: ms:
214.5	215.489	215.062	215.566	214.575	212.112	215.346	213.725	2e-05
total time  consumed by all 9 thread: 1716.38 ms
=======================================================================================
compute start at 4294967293, end at 4294967295 by thread id 0x101e64000 thread run id 7 time clock use:3e-05
compute start at 1, end at 613566756 by thread id 0x101acf000 thread run id 0 time clock use:213.747
compute start at 3067833781, end at 3681400536 by thread id 0x101d5e000 thread run id 5 time clock use:214.239
compute start at 2454267025, end at 3067833780 by thread id 0x101cdb000 thread run id 4 time clock use:214.62
compute start at 1227133513, end at 1840700268 by thread id 0x101bd5000 thread run id 2 time clock use:215.162
compute start at 613566757, end at 1227133512 by thread id 0x101b52000 thread run id 1 time clock use:215.282
compute start at 1840700269, end at 2454267024 by thread id 0x101c58000 thread run id 3 time clock use:215.544
compute start at 3681400537, end at 4294967292 by thread id 0x101de1000 thread run id 6 time clock use:215.183
---------------------------------------------------------------------------------
max number 4294967295 | 8 thread counts| average number 613566756
time consumption details: ms:
213.747	215.282	215.162	215.544	214.62	214.239	215.183	3e-05
total time  consumed by all 8 thread: 1503.78 ms
=======================================================================================
compute start at 4294967293, end at 4294967295 by thread id 0x101de1000 thread run id 6 time clock use:3e-05
compute start at 1431655765, end at 2147483646 by thread id 0x101bd5000 thread run id 2 time clock use:215.706
compute start at 2863311529, end at 3579139410 by thread id 0x101cdb000 thread run id 4 time clock use:215.906
compute start at 1, end at 715827882 by thread id 0x101acf000 thread run id 0 time clock use:216.341
compute start at 3579139411, end at 4294967292 by thread id 0x101d5e000 thread run id 5 time clock use:216.136
compute start at 715827883, end at 1431655764 by thread id 0x101b52000 thread run id 1 time clock use:216.631
compute start at 2147483647, end at 2863311528 by thread id 0x101c58000 thread run id 3 time clock use:216.74
---------------------------------------------------------------------------------
max number 4294967295 | 7 thread counts| average number 715827882
time consumption details: ms:
216.341	216.631	215.706	216.74	215.906	216.136	3e-05
total time  consumed by all 7 thread: 1297.46 ms
=======================================================================================
compute start at 858993460, end at 1717986918 by thread id 0x101b52000 thread run id 1 time clock use:214.123
compute start at 2576980378, end at 3435973836 by thread id 0x101c58000 thread run id 3 time clock use:214.291
compute start at 1717986919, end at 2576980377 by thread id 0x101bd5000 thread run id 2 time clock use:214.571
compute start at 1, end at 858993459 by thread id 0x101acf000 thread run id 0 time clock use:214.591
compute start at 3435973837, end at 4294967295 by thread id 0x101cdb000 thread run id 4 time clock use:214.405
---------------------------------------------------------------------------------
max number 4294967295 | 5 thread counts| average number 858993459
time consumption details: ms:
214.591	214.123	214.571	214.291	214.405
total time  consumed by all 5 thread: 1071.98 ms
=======================================================================================
compute start at 4294967293, end at 4294967295 by thread id 0x101cdb000 thread run id 4 time clock use:3e-05
compute start at 1, end at 1073741823 by thread id 0x101acf000 thread run id 0 time clock use:213.735
compute start at 2147483647, end at 3221225469 by thread id 0x101bd5000 thread run id 2 time clock use:215.136
compute start at 3221225470, end at 4294967292 by thread id 0x101c58000 thread run id 3 time clock use:215.164
compute start at 1073741824, end at 2147483646 by thread id 0x101b52000 thread run id 1 time clock use:216.137
---------------------------------------------------------------------------------
max number 4294967295 | 5 thread counts| average number 1073741823
time consumption details: ms:
213.735	216.137	215.136	215.164	3e-05
total time  consumed by all 5 thread: 860.172 ms
=======================================================================================
compute start at 1431655766, end at 2863311530 by thread id 0x101b52000 thread run id 1 time clock use:196.981
compute start at 1, end at 1431655765 by thread id 0x101acf000 thread run id 0 time clock use:197.728
compute start at 2863311531, end at 4294967295 by thread id 0x101bd5000 thread run id 2 time clock use:198.393
---------------------------------------------------------------------------------
max number 4294967295 | 3 thread counts| average number 1431655765
time consumption details: ms:
197.728	196.981	198.393
total time  consumed by all 3 thread: 593.102 ms
=======================================================================================
compute start at 4294967295, end at 4294967295 by thread id 0x101bd5000 thread run id 2 time clock use:1e-05
compute start at 1, end at 2147483647 by thread id 0x101acf000 thread run id 0 time clock use:171.264
compute start at 2147483648, end at 4294967294 by thread id 0x101b52000 thread run id 1 time clock use:171.393
---------------------------------------------------------------------------------
max number 4294967295 | 3 thread counts| average number 2147483647
time consumption details: ms:
171.264	171.393	1e-05
total time  consumed by all 3 thread: 342.657 ms
=======================================================================================
compute start at 1, end at 4294967295 by thread id 0x101acf000 thread run id 0 time clock use:168.744
---------------------------------------------------------------------------------
max number 4294967295 | 1 thread counts| average number 4294967295
time consumption details: ms:
168.744
total time  consumed by all 1 thread: 168.744 ms
=======================================================================================

不要以为 使用的计算资源越多耗费的时间 就越长,正好相反,使用的诸资源越多,总的计算时间越短。所以这里统计出来的数据 可以当成计算资源的使用量而不是计算时间的耗费量,这也映证了为什么看到越小,完成计算的时间感觉长的事实。
对于

getElapsedTime

这个函数是根据 cpu 的 ticks 数量来计算的所谓的 elapse time,这个应该理解为计算资源的使用,而由于这个 ticks 转化过来的并不能完全认为是计算时间,甚至还只能说是计算资源的使用量。 对于同一个数据运算,更多的计算资源会带来越短的并行计算时间,但是总的线性是变大的,即所有 thread 的计算时间和。
如果要统计 app 计算时间 的耗费情况,可以 time 来统计:

time  app

time 实际统计的效果如下:

JialinWus-MacBook-Pro:c_thread_compute_numbers Jialin$ time ./MainExecution 1
compute start at 1, end at 4294967295 by thread id 0x105c0c000 thread run id 0 time clock use:168.654
---------------------------------------------------------------------------------
max number 4294967295 | 1 thread counts| average number 4294967295
time consumption details: ms:
168.654
total time  consumed by all 1 thread: 168.654 ms
=======================================================================================

real	0m16.907s
user	0m16.831s
sys	0m0.037s
JialinWus-MacBook-Pro:c_thread_compute_numbers Jialin$ time ./MainExecution 5
compute start at 1, end at 858993459 by thread id 0x109777000 thread run id 0 time clock use:215.805
compute start at 858993460, end at 1717986918 by thread id 0x1097fa000 thread run id 1 time clock use:216.058
compute start at 1717986919, end at 2576980377 by thread id 0x10987d000 thread run id 2 time clock use:216.758
compute start at 3435973837, end at 4294967295 by thread id 0x109983000 thread run id 4 time clock use:216.799
compute start at 2576980378, end at 3435973836 by thread id 0x109900000 thread run id 3 time clock use:217.048
---------------------------------------------------------------------------------
max number 4294967295 | 5 thread counts| average number 858993459
time consumption details: ms:
215.805	216.058	216.758	217.048	216.799
total time  consumed by all 5 thread: 1082.47 ms
=======================================================================================

real	0m5.707s
user	0m21.670s
sys	0m0.038s
JialinWus-MacBook-Pro:c_thread_compute_numbers Jialin$ time ./MainExecution 10
compute start at 4294967291, end at 4294967295 by thread id 0x101775000 thread run id 10 time clock use:1e-05
compute start at 3435973833, end at 3865470561 by thread id 0x10166f000 thread run id 8 time clock use:213.516
compute start at 429496730, end at 858993458 by thread id 0x1012da000 thread run id 1 time clock use:215.401
compute start at 1, end at 429496729 by thread id 0x101257000 thread run id 0 time clock use:216.173
compute start at 858993459, end at 1288490187 by thread id 0x10135d000 thread run id 2 time clock use:216.422
compute start at 1288490188, end at 1717986916 by thread id 0x1013e0000 thread run id 3 time clock use:216.882
compute start at 2147483646, end at 2576980374 by thread id 0x1014e6000 thread run id 5 time clock use:216.682
compute start at 3006477104, end at 3435973832 by thread id 0x1015ec000 thread run id 7 time clock use:216.527
compute start at 3865470562, end at 4294967290 by thread id 0x1016f2000 thread run id 9 time clock use:216.56
compute start at 1717986917, end at 2147483645 by thread id 0x101463000 thread run id 4 time clock use:217.095
compute start at 2576980375, end at 3006477103 by thread id 0x101569000 thread run id 6 time clock use:217.086
---------------------------------------------------------------------------------
max number 4294967295 | 11 thread counts| average number 429496729
time consumption details: ms:
216.173	215.401	216.422	216.882	217.095	216.682	217.086	216.527	213.516	216.56	1e-05
total time  consumed by all 11 thread: 2162.34 ms
=======================================================================================

real	0m5.678s
user	0m21.714s
sys	0m0.036s
JialinWus-MacBook-Pro:c_thread_compute_numbers Jialin$ time ./MainExecution 15
compute start at 2290649225, end at 2576980377 by thread id 0x10dccf000 thread run id 8 time clock use:205.916
compute start at 1431655766, end at 1717986918 by thread id 0x10db46000 thread run id 5 time clock use:208.628
compute start at 2004318072, end at 2290649224 by thread id 0x10dc4c000 thread run id 7 time clock use:211.685
compute start at 2863311531, end at 3149642683 by thread id 0x10ddd5000 thread run id 10 time clock use:212.801
compute start at 3722304990, end at 4008636142 by thread id 0x10df5e000 thread run id 13 time clock use:212.565
compute start at 1717986919, end at 2004318071 by thread id 0x10dbc9000 thread run id 6 time clock use:215.262
compute start at 1145324613, end at 1431655765 by thread id 0x10dac3000 thread run id 4 time clock use:215.788
compute start at 286331154, end at 572662306 by thread id 0x10d93a000 thread run id 1 time clock use:216.167
compute start at 858993460, end at 1145324612 by thread id 0x10da40000 thread run id 3 time clock use:215.954
compute start at 4008636143, end at 4294967295 by thread id 0x10dfe1000 thread run id 14 time clock use:214.746
compute start at 1, end at 286331153 by thread id 0x10d8b7000 thread run id 0 time clock use:216.804
compute start at 2576980378, end at 2863311530 by thread id 0x10dd52000 thread run id 9 time clock use:216.216
compute start at 3435973837, end at 3722304989 by thread id 0x10dedb000 thread run id 12 time clock use:215.92
compute start at 572662307, end at 858993459 by thread id 0x10d9bd000 thread run id 2 time clock use:217.311
compute start at 3149642684, end at 3435973836 by thread id 0x10de58000 thread run id 11 time clock use:216.342
---------------------------------------------------------------------------------
max number 4294967295 | 15 thread counts| average number 286331153
time consumption details: ms:
216.804	216.167	217.311	215.954	215.788	208.628	215.262	211.685	205.916	216.216	212.801	216.342	215.92	212.565	214.746
total time  consumed by all 15 thread: 3212.11 ms
=======================================================================================

real	0m5.697s
user	0m21.699s
sys	0m0.041s
JialinWus-MacBook-Pro:c_thread_compute_numbers Jialin$ time ./MainExecution 20
compute start at 4294967281, end at 4294967295 by thread id 0x104dd2000 thread run id 20 time clock use:5e-05
compute start at 429496729, end at 644245092 by thread id 0x10449c000 thread run id 2 time clock use:210.18
compute start at 1073741821, end at 1288490184 by thread id 0x104625000 thread run id 5 time clock use:211.157
compute start at 3221225461, end at 3435973824 by thread id 0x104b43000 thread run id 15 time clock use:210.051
compute start at 2791728733, end at 3006477096 by thread id 0x104a3d000 thread run id 13 time clock use:210.697
compute start at 3865470553, end at 4080218916 by thread id 0x104ccc000 thread run id 18 time clock use:210.2
compute start at 1503238549, end at 1717986912 by thread id 0x10472b000 thread run id 7 time clock use:213.612
compute start at 2576980369, end at 2791728732 by thread id 0x1049ba000 thread run id 12 time clock use:213.288
compute start at 2362232005, end at 2576980368 by thread id 0x104937000 thread run id 11 time clock use:213.848
compute start at 214748365, end at 429496728 by thread id 0x104419000 thread run id 1 time clock use:215.585
compute start at 2147483641, end at 2362232004 by thread id 0x1048b4000 thread run id 10 time clock use:214.767
compute start at 1, end at 214748364 by thread id 0x104396000 thread run id 0 time clock use:215.983
compute start at 644245093, end at 858993456 by thread id 0x10451f000 thread run id 3 time clock use:216.075
compute start at 1717986913, end at 1932735276 by thread id 0x1047ae000 thread run id 8 time clock use:215.855
compute start at 1288490185, end at 1503238548 by thread id 0x1046a8000 thread run id 6 time clock use:216.339
compute start at 3006477097, end at 3221225460 by thread id 0x104ac0000 thread run id 14 time clock use:215.373
compute start at 3650722189, end at 3865470552 by thread id 0x104c49000 thread run id 17 time clock use:215.352
compute start at 858993457, end at 1073741820 by thread id 0x1045a2000 thread run id 4 time clock use:216.961
compute start at 4080218917, end at 4294967280 by thread id 0x104d4f000 thread run id 19 time clock use:215.355
compute start at 1932735277, end at 2147483640 by thread id 0x104831000 thread run id 9 time clock use:216.771
compute start at 3435973825, end at 3650722188 by thread id 0x104bc6000 thread run id 16 time clock use:216.083
---------------------------------------------------------------------------------
max number 4294967295 | 21 thread counts| average number 214748364
time consumption details: ms:
215.983	215.585	210.18	216.075	216.961	211.157	216.339	213.612	215.855	216.771	214.767	213.848	213.288	210.697	215.373	210.051	216.083	215.352	210.2	215.355	5e-05
total time  consumed by all 21 thread: 4283.53 ms
=======================================================================================

real	0m5.667s
user	0m21.728s
sys	0m0.038s
JialinWus-MacBook-Pro:c_thread_compute_numbers Jialin$ time ./MainExecution 25
compute start at 4294967276, end at 4294967295 by thread id 0x1059c3000 thread run id 25 time clock use:4e-05
compute start at 3779571203, end at 3951369893 by thread id 0x10583a000 thread run id 22 time clock use:203.115
compute start at 687194765, end at 858993455 by thread id 0x104f04000 thread run id 4 time clock use:211.459
compute start at 1, end at 171798691 by thread id 0x104cf8000 thread run id 0 time clock use:213.067
compute start at 2748779057, end at 2920577747 by thread id 0x105528000 thread run id 16 time clock use:211.635
compute start at 2233382984, end at 2405181674 by thread id 0x10539f000 thread run id 13 time clock use:212.498
compute start at 858993456, end at 1030792146 by thread id 0x104f87000 thread run id 5 time clock use:213.514
compute start at 1202590838, end at 1374389528 by thread id 0x10508d000 thread run id 7 time clock use:213.457
compute start at 171798692, end at 343597382 by thread id 0x104d7b000 thread run id 1 time clock use:213.919
compute start at 2405181675, end at 2576980365 by thread id 0x105422000 thread run id 14 time clock use:212.842
compute start at 1374389529, end at 1546188219 by thread id 0x105110000 thread run id 8 time clock use:213.856
compute start at 2920577748, end at 3092376438 by thread id 0x1055ab000 thread run id 17 time clock use:213.056
compute start at 1889785602, end at 2061584292 by thread id 0x105299000 thread run id 11 time clock use:213.777
compute start at 2061584293, end at 2233382983 by thread id 0x10531c000 thread run id 12 time clock use:213.737
compute start at 1546188220, end at 1717986910 by thread id 0x105193000 thread run id 9 time clock use:213.992
compute start at 3951369894, end at 4123168584 by thread id 0x1058bd000 thread run id 23 time clock use:212.695
compute start at 1717986911, end at 1889785601 by thread id 0x105216000 thread run id 10 time clock use:214.358
compute start at 1030792147, end at 1202590837 by thread id 0x10500a000 thread run id 6 time clock use:215.057
compute start at 3435973821, end at 3607772511 by thread id 0x105734000 thread run id 20 time clock use:213.541
compute start at 3607772512, end at 3779571202 by thread id 0x1057b7000 thread run id 21 time clock use:213.914
compute start at 515396074, end at 687194764 by thread id 0x104e81000 thread run id 3 time clock use:215.935
compute start at 2576980366, end at 2748779056 by thread id 0x1054a5000 thread run id 15 time clock use:214.58
compute start at 343597383, end at 515396073 by thread id 0x104dfe000 thread run id 2 time clock use:216.11
compute start at 4123168585, end at 4294967275 by thread id 0x105940000 thread run id 24 time clock use:213.872
compute start at 3092376439, end at 3264175129 by thread id 0x10562e000 thread run id 18 time clock use:214.481
compute start at 3264175130, end at 3435973820 by thread id 0x1056b1000 thread run id 19 time clock use:214.574
---------------------------------------------------------------------------------
max number 4294967295 | 26 thread counts| average number 171798691
time consumption details: ms:
213.067	213.919	216.11	215.935	211.459	213.514	215.057	213.457	213.856	213.992	214.358	213.777	213.737	212.498	212.842	214.58	211.635	213.056	214.481	214.574	213.541	213.914	203.115	212.695	213.872	4e-05
total time  consumed by all 26 thread: 5333.04 ms
=======================================================================================

real	0m5.745s
user	0m21.606s
sys	0m0.039s
JialinWus-MacBook-Pro:c_thread_compute_numbers Jialin$ time ./MainExecution 30
compute start at 4294967281, end at 4294967295 by thread id 0x10cd01000 thread run id 30 time clock use:2e-05
compute start at 3149642673, end at 3292808248 by thread id 0x10c8e9000 thread run id 22 time clock use:201.107
compute start at 429496729, end at 572662304 by thread id 0x10bf30000 thread run id 3 time clock use:206.875
compute start at 1861152489, end at 2004318064 by thread id 0x10c44e000 thread run id 13 time clock use:206.927
compute start at 2433814793, end at 2576980368 by thread id 0x10c65a000 thread run id 17 time clock use:206.709
compute start at 3722304977, end at 3865470552 by thread id 0x10caf5000 thread run id 26 time clock use:207.031
compute start at 1, end at 143165576 by thread id 0x10bda7000 thread run id 0 time clock use:211.236
compute start at 3292808249, end at 3435973824 by thread id 0x10c96c000 thread run id 23 time clock use:209.218
compute start at 3865470553, end at 4008636128 by thread id 0x10cb78000 thread run id 27 time clock use:208.838
compute start at 3435973825, end at 3579139400 by thread id 0x10c9ef000 thread run id 24 time clock use:209.57
compute start at 572662305, end at 715827880 by thread id 0x10bfb3000 thread run id 4 time clock use:211.773
compute start at 286331153, end at 429496728 by thread id 0x10bead000 thread run id 2 time clock use:213.848
compute start at 4151801705, end at 4294967280 by thread id 0x10cc7e000 thread run id 29 time clock use:210.899
compute start at 2004318065, end at 2147483640 by thread id 0x10c4d1000 thread run id 14 time clock use:213.265
compute start at 4008636129, end at 4151801704 by thread id 0x10cbfb000 thread run id 28 time clock use:211.808
compute start at 1574821337, end at 1717986912 by thread id 0x10c348000 thread run id 11 time clock use:214.082
compute start at 2147483641, end at 2290649216 by thread id 0x10c554000 thread run id 15 time clock use:213.993
compute start at 3579139401, end at 3722304976 by thread id 0x10ca72000 thread run id 25 time clock use:212.886
compute start at 143165577, end at 286331152 by thread id 0x10be2a000 thread run id 1 time clock use:215.643
compute start at 1717986913, end at 1861152488 by thread id 0x10c3cb000 thread run id 12 time clock use:214.748
compute start at 715827881, end at 858993456 by thread id 0x10c036000 thread run id 5 time clock use:215.875
compute start at 2863311521, end at 3006477096 by thread id 0x10c7e3000 thread run id 20 time clock use:214.258
compute start at 2576980369, end at 2720145944 by thread id 0x10c6dd000 thread run id 18 time clock use:214.538
compute start at 1002159033, end at 1145324608 by thread id 0x10c13c000 thread run id 7 time clock use:215.924
compute start at 2720145945, end at 2863311520 by thread id 0x10c760000 thread run id 19 time clock use:214.671
compute start at 1431655761, end at 1574821336 by thread id 0x10c2c5000 thread run id 10 time clock use:214.762
compute start at 3006477097, end at 3149642672 by thread id 0x10c866000 thread run id 21 time clock use:214.568
compute start at 2290649217, end at 2433814792 by thread id 0x10c5d7000 thread run id 16 time clock use:215.108
compute start at 1145324609, end at 1288490184 by thread id 0x10c1bf000 thread run id 8 time clock use:216.055
compute start at 858993457, end at 1002159032 by thread id 0x10c0b9000 thread run id 6 time clock use:216.336
compute start at 1288490185, end at 1431655760 by thread id 0x10c242000 thread run id 9 time clock use:215.994
---------------------------------------------------------------------------------
max number 4294967295 | 31 thread counts| average number 143165576
time consumption details: ms:
211.236	215.643	213.848	206.875	211.773	215.875	216.336	215.924	216.055	215.994	214.762	214.082	214.748	206.927	213.265	213.993	215.108	206.709	214.538	214.671	214.258	214.568	201.107	209.218	209.57	212.886	207.031	208.838	211.808	210.899	2e-05
total time  consumed by all 31 thread: 6368.55 ms
=======================================================================================

real	0m5.718s
user	0m21.638s
sys	0m0.041s

可以看到用 time 统计出来的数据时间的大小(包括 系统时间 与 用户时间) 和实际感觉的一样,这可以当成是 总的计算所耗费的并行时间

2015-01-06