机器是宏碁的 4741G 的 I5 版本,基本情况是 i5 430M / 2G
虚拟机软件使用的是 VirtualBox 3.2.8
主机系统的 win7 64位
虚拟机是 ubuntu 10.04 32位
主机编译器是 vc10 编译参数是 cl /EHsc /openmp /O2 bench.cpp
虚拟机编译器是 g++ 4.4 编译参数是 g++ –fopenmp –O2 –obench bench.cpp
测试的主要算法为累加,以下是测试的结果
线程数 | 主机2核,超线程 | 虚拟4核,启用VT | 虚拟1核,关闭VT |
1 | 9.762 | 10.466 | 11.465 |
2 | 4.921 | 5.302 | 10.793 |
4 | 2.749 | 2.996 | 11.519 |
1 | 100.00% | 93.27% | 85.15% |
2 | 100.00% | 92.81% | 45.59% |
4 | 100.00% | 91.76% | 23.86% |
注:VT关闭时,虚拟机只能为单核
从测试的结果上看来,能得出如下的初步结论
以下是测试程序的源代码
#include <omp.h> #include <stdio.h> #include <stdlib.h> #include <math.h> void show_usage() { printf("bench max_nubmer [thread_count]/n"); } int main(int argc, char * argv[]) { long long max_nubmer = 0; double total, part; int threads = 2; int threads_used = 0; int mode = 0; long long i; if (argc == 1) { show_usage(); return 0; } if (argc > 1) //max_nubmer = atoll(argv[1]); // for linux max_nubmer = _atoi64(argv[1]); if (argc > 2) threads = atoi(argv[2]); omp_set_num_threads(threads); #pragma omp parallel default(none) private(i, part) shared(total, threads_used, mode, max_nubmer) { #pragma omp master threads_used = omp_get_num_threads(); part = 0; #pragma omp for for(i = 1; i < max_nubmer; i++) { if (mode == 0) part += (double)i; } total += part; } char modes[] = {'+', '*'}; printf("%d thread required/n", threads); printf("%d thread used/n", threads_used); printf("do %c %lld times/n", modes[mode], max_nubmer); printf("result is %f/n", total); return 0; };