最近在看<<c++ Footprint and Performance Optimization>>贴计时函数源代码于此

booktools.h

#ifndef __BOOKTOOLS_H
#define __BOOKTOOLS_H
#if __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <ctype.h>

unsigned long time_fn(long (*fn)(void), int nrSamples = 1);
//void time_on();
//unsigned long time_off();
void print_time(long msec);

void system_info();
//void alignment_info();
void DisplayASCIITable();

#if __cplusplus
}
#endif

#endif
booktools.cpp

#if __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include "BookTools.h"

#ifdef _MSC_VER
#include<ctime>
unsigned long time_fn(long (*fn)(void), int nrSamples)
{
 unsigned long average = 0;
 clock_t tBegin, tEnd;

 for (int i = 0; i < nrSamples; i++)
 {
  tBegin = clock();
  fn();
  tEnd = clock();

  average += tEnd - tBegin;
 }

 return ((unsigned long)average / nrSamples);
}

#else
#include<time.h>
unsigned long time_fn(long (*fn)(void), int nrSamples)
{
 unsigned long average = 0;
 clock_t tBegin, tEnd;

 for (int i = 0; i < nrSamples; i++)
 {
  tBegin = clock();
  fn();
  tEnd = clock();

  average += tEnd - tBegin;
 }

 return ((unsigned long)average / (nrSamples*1000));
}
#endif

void print_time(long msec)
{
    int hour, min, sec;

    hour = (int)(msec / (60*60*1000));
  msec = msec % (60*60*1000);

    min = (int)(msec / (60*1000));
  msec = msec % (60*1000);

    sec = (int)(msec / (1000));
  msec = msec % (1000);

  printf("%02dh %02dm %02ds %03dms/n", hour, min, sec, msec);
}

struct smallstr {char q;};
struct bit {unsigned q: 1;};
union  un  {char a;};

void system_info()
{
 printf("Sizeof char        : %d/n", sizeof(char));
 printf("Sizeof short       : %d/n", sizeof(short));
 printf("Sizeof short int   : %d/n", sizeof(short int));
 printf("Sizeof long        : %d/n", sizeof(long));
 printf("Sizeof int         : %d/n", sizeof(int));
 printf("Sizeof float       : %d/n", sizeof(float));
 printf("Sizeof double      : %d/n", sizeof(double));
 printf("Sizeof long double : %d/n", sizeof(long double));
#ifdef _MSC_VER
 printf("Sizeof int64       : %d/n", sizeof(__int64));
#else
 printf("Sizeof long long   : %d/n", sizeof(long long));
#endif
 printf("Min sizeof struct  : %d/n", sizeof(struct smallstr));
 printf("Min sizeof bitfield: %d/n", sizeof(struct bit));
 printf("Min sizeof union   : %d/n", sizeof(un));
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值