- 论坛徽章:
- 0
|
是不是用多进程比多线程快得多?
在一个8CPU的AIX上面,直接使用线程比使用进程整整慢了三倍
,为什么?
下面thread5直接使用线程,thread6使用进程
thread5执行时间为:
开始时间:Fri Nov 5 08:23:43 CST 2004
结束时间:Fri Nov 5 08:25:17 CST 2004
thread6执行时间为:
Fri Nov 5 08:22:43 CST 2004
Fri Nov 5 08:23:11 CST 2004
附上源代码:
- 程序 thread5.c:
- #include<pthread.h>;
- #include<stdlib.h>;
- #include<unistd.h>;
- #include<stdio.h>;
- /*计算20000! */
- void *fac(void *arg){
- int i,j,k,r;
- int n;
- int data[200000];
- int digit;
- data[0]=1;
- data[1]=1;
- digit=1;
- n=20000;
- for(i=1;i<n+1;i++){
- for(j=1;j<digit+1;j++)data[j]*=i;
- for(j=1;j<digit+1;j++){
- if (data[j]>;=10){
- for(r=j;r<digit+1;r++){
- if (data[digit]>;=10)
- digit++;
- data[r+1]+=data[r]/10;
- data[r]=data[r]%10;
- }
- }
- }
- }
- }
- int main(void){
- system("date");
- pthread_t mythread1,mythread2,mythread3;
- pthread_create(&mythread1,NULL,fac,NULL);
- pthread_create(&mythread2,NULL,fac,NULL);
- pthread_create(&mythread3,NULL,fac,NULL);
- pthread_join(mythread1,NULL);
- pthread_join(mythread2,NULL);
- pthread_join(mythread3,NULL);
- system("date");
- printf("done\n");
- exit(0);
- }
- 程序thread6.c:
- #include<pthread.h>;
- #include<stdlib.h>;
- #include<unistd.h>;
- #include<stdio.h>;
- void *fac(void *arg){
- system("./fac1");
- }
- int main(void){
- system("date");
- pthread_t
- mythread1,mythread2,mythread3,mythread4,mythread5,mythre
- ad6,mythread7,mythread8;
- pthread_create(&mythread1,NULL,fac,NULL);
- pthread_create(&mythread2,NULL,fac,NULL);
- pthread_create(&mythread3,NULL,fac,NULL);
- pthread_join(mythread1,NULL);
- pthread_join(mythread2,NULL);
- pthread_join(mythread3,NULL);
- system("date");
- printf("done\n");
- exit(0);
- }
- 其中fac1.c为:
- //计算20000!
- #include <stdio.h>;
- void main(){
- int i,j,k,r;
- int data[20000000];
- int n;
- int digit;
- for (i=1;i<20000000+1;i++)data[i]=0;
- data[0]=1;
- data[1]=1;
- digit=1;
- n=20000;
- for(i=1;i<n+1;i++){
- for(j=1;j<digit+1;j++)data[j]*=i;
- for(j=1;j<digit+1;j++){
- if (data[j]>;=10){
- for(r=j;r<digit+1;r++){
- if (data[digit]>;=10)
- digit++;
- data[r+1]+=data[r]/10;
- data[r]=data[r]%10;
- }
- }
- }
- }
- }
复制代码 |
|