首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

pthread,统计执行时间的有关问题

2013-01-06 
pthread,统计执行时间的问题/*启动线程并行计算行列式*/void compute_det(){int* thread_id new int[thr

pthread,统计执行时间的问题


/*启动线程并行计算行列式*/
void compute_det()
{
int* thread_id = new int[thread_num];
for(int i=0;i<thread_num;i++)
thread_id[i] = i;
for(int i=0;i<thread_num;i++){
int* t = thread_id+i;
pthread_create(&threads[i],NULL,cacu_thread,(void*)t);
}
for(int i=0;i<thread_num;i++)
pthread_join(threads[i],NULL);
}



    /*主函数中调用计算行列式的值*/
    det_value = 0.0;
    clock_t stime,etime;
   stime = clock();
   cout<<stime<<endl;
compute_det();
for(int i=0;i<thread_num;i++)
det_value += private_det_value[i];
etime = clock();
cout<<etime<<endl;
    cout<<"The determinant's value is "<<det_value<<"."<<endl;
double time_used = ((double)(etime - stime)) / CLOCKS_PER_SEC;


程序可以执行。在计算10阶行列式时,启动1个线程大约用时3秒(大致和人为数的差不多)。
但是在用10个线程并行计算时,统计结果却是8秒多,而我心数明显地感觉只有1秒不到的时间。
也就是说,时间统计出了错。路过大虾们,错在何处?怎么改?谢谢啦!
[解决办法]
仅供参考
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
    #include <io.h>
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
char logstr[16000];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;

    if (NULL==pszFmt
[解决办法]
0==pszFmt[0]) return;
    if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );


    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
            flog=fopen(logfilename1,"a");
            if (NULL==flog) return;
        }
        fclose(flog);
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
    int i;
#ifdef WIN32
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
#endif
    for (i=0;i<10000;i++) {
        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
    }
#ifdef WIN32
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}


[解决办法]
提醒:
使用电脑计时有时误差会很大,因为待测程序段的运行会影响电脑时钟。
将待测程序段循环足够多次,手动掐秒表计时可能更准确。

[解决办法]
将待测程序段循环足够多次,手动掐秒表计时可能更准确。

热点排行