dahdi_tools 分析(二)dahdi_speed

dahdi_tools 分析(二)dahdi_speed

dahdi_speed 使用

# dahdi_speed 
Count: 0

dahdi_speed 运行一定时间后,打印 Count: 0 。具体什么用途需要看代码。

源码分析

#include <stdio.h>
#include <sys/signal.h>
#include <unistd.h>
#include <stdlib.h>

static long count=0;

static void alm(int sig)
{
    
    
	printf("Count: %ld\n", count);
	exit(0);
}


int main(int argc, char *argv[])
{
    
    
	int a=0,b=0,c;
	signal(SIGALRM, alm);    // SIGALRM 信号处理函数设置为 alm
	alarm(5);                // 5 秒后产生 SIGALRM
	for (;;) {
    
    
		for (c=0;c<1000;c++)      // 执行1000次乘法运算
			a = a * b;
		count++;                  // 每执行1000次乘法运算,计数+1
	}
}

由代码可以看出,dahdi_speed 用于测试cpu 5秒内能执行多少千次乘法运算。

目前我在多个平台上测试都是 Count: 0 ,按道理不应该啊。 5 秒内还不能运行1000次乘法运算?

我在自己的测试机上单独编译了一个:

gcc -o speed speed.c

运行结果如下:

$ ./speed 
Count: 1324926

这个才符合预期嘛!

那为什么 dahdi_tools 里面编译出来是 Count: 0 呢?问题出现在Makefile 里。

CFLAGS = -g -Wall -O2 $(DAHDI_INCLUDE) $(am__append_1)

这里开启了 -g 选项用于gdb 调试的,这样使代码效率变低了。

作为验证,我单独编译时加上 -g -O2

gcc -o speed speed.c -g -O2

然后运行

$ ./speed 
Count: 0

果然!!! -g 选项要慎用!

小结

1.dahdi_speed 用于计算cpu 在5秒内运行1000次乘法运算的次数。如 Count : 8 。表示 5秒内运行了8000次乘法运算。

2.由于dahdi_tools 中的Makefile 默认加了 -g 选项编译,所以使程序效率很慢,执行时结果通常为 Count : 0。

猜你喜欢

转载自blog.csdn.net/agave7/article/details/107260593