Unix-Center 机器速度

阅读更多

最近无聊的很,突然心血来潮想测试一下 Unix 体验中心几台机器的运行速度。测试方式很简单,就是我在幼儿园深造期间,大人们用来衡量我们这些小屁孩智力程度的办法——数数,不过现在要让 CPU 来数数,看看从 0 数到 2^30 - 1 要多长时间


#include 
#include 
#include 
#include 

int
main()
{
	int		i;
	unsigned char	hash_128bit[16];
	unsigned char  *ptrhash = hash_128bit;
	struct timeval	tpstart, tpend;
	float		timeuse;
	gettimeofday(&tpstart, NULL);
	memset(ptrhash, '\0', 16);
	/*
	 *如果循环终止条件改成 hash_128bit[16] ^ 0xFF,
	 *从测试数 1G 个数的时间来看,以现有的计算机速度,
	 *估计到宇宙毁灭时,循环也不会结束,可见暴力破解 MD5 是不可行的^^
	 */
	while (hash_128bit[3] ^ 0x3F) {
		i = 0;
		while (i < 16) {
			hash_128bit[i] += 1;
			if (hash_128bit[i] != 0) {
				break;
			} else {
				i++;
			}
		}
		/*printf("%d,", hash_128bit[i]);*/
	}
	gettimeofday(&tpend, NULL);
	timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec)
	    + tpend.tv_usec - tpstart.tv_usec;
	printf("%fS\n", timeuse / 1000000);
	return 0;
}



最终测试结果出人意料,这些 CPU 数数的速度还不如我自己的虚拟机快。这是怎么一回事呢?尤其是 X4100 这台机器,使用2 颗双核单线程的AMD Opteron 280芯片,CPU 主频为2.4 GHz,居然要 10s 才能数完 1G 个数,比我的虚拟机多一倍的时间。费解……

你可能感兴趣的:(Unix,FreeBSD,虚拟机,C,C++)