zoukankan      html  css  js  c++  java
  • 第六周测试补交-sumN

    测试内容:1-N求和

    实验代码:

    #include<stdio.h>
    #include<stdlib.h>
    #include<pthread.h>
    #define MAX     1000000000    //整数范围 1 ~ MAX
    #define N       100           //创建N 个子线程求和
    #define AVE     (MAX/N)       //每个子线程处理的整数个数
    long long     *sum = NULL;    //保存各个子线程计算的结果
    //获取当前时间
    double get_time()
    {    
    	struct timeval t;   
    	gettimeofday(&t,NULL);   
    	return t.tv_sec + t.tv_usec/1000000.0;
    }
    //求和子线程
    void* sum_work(void* arg)
    {    
    	int n = (int)arg;  //第n部分    
    	long long start = n*AVE+1;    
    	long long end = start + AVE -1;    
    	long long i;    
    	sum[n] = 0;   
    	//计算start ~ end 范围的整数和   
    	for(i=start; i <= end;i++)    
    	{        
    		sum[n] = sum[n] + i;    
    	}    
    	pthread_exit(0);
    }
    int main()
    {    
    	double         t1,t2;    
    	pthread_t      *pthread_id = NULL; //保存子线程id    
    	int            i;    
    	long long      result = 0;         //总和    
    	pthread_id = (pthread_t*)malloc(N * sizeof(pthread_t));   
    	sum  = (long long*)malloc(N * sizeof(long long));
        //开始计算   
    	t1 = get_time();    
    	//创建N个子线程   
    	for(i=0;i<N;i++)
        {        
    		pthread_create(pthread_id+i,NULL,sum_work,i);    
    	}   
    	//将各个子线程的求和结果综合到一起   
    	for(i=0;i<N;i++)
        {        
    	//等待子线程结束,如果该子线程已经结束,则立即返回        
    	pthread_join(pthread_id[i],NULL);       
    	result += sum[i];
        }
        //求和结束   
    	t2 = get_time();
        //输出求和结果和运行时间    
    	printf("sum of 1 ~ %lld is %lld runtime is %f
    ",(long long)MAX,result,t2-t1);   
    	free(pthread_id);   
    	free(sum);
    	return 0;
    }
    

    结果截图:

  • 相关阅读:
    备忘录方法与动态规划比较
    struct大小
    位域
    cocos2d-x获得系统的语言
    游戏中的心理学:利用归因错误让玩家更爱你的游戏
    武侠游戏核心追求点
    将“Cocos2dx-截屏并设置图片尺寸 ”中cocos2d-x代码转换为2.2的代码
    Cocos2dx-截屏并设置图片尺寸
    信息图:iOS 7开发者需要知道的事
    批处理学习 二
  • 原文地址:https://www.cnblogs.com/liangxu111/p/11958645.html
Copyright © 2011-2022 走看看