1 // ConsoleApplication.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
2 //
3 //#include "StdAfx.h"
4 #include "windows.h"
5 #include "iostream"
6 #include "string"
7 #include<iostream>
8 #include<cstdlib>
9 #include<ctime>
10 using namespace std;
11
12 //用来存储信息
13 DWORD deax;
14 DWORD debx;
15 DWORD decx;
16 DWORD dedx;
17
18 void ExeCPUID(DWORD veax)//初始化CPU
19 {
20 __asm
21 {
22 mov eax, veax
23 cpuid
24 mov deax, eax
25 mov debx, ebx
26 mov decx, ecx
27 mov dedx, edx
28 }
29 }
30
31 long GetCPUFreq()//获取CPU频率,单位: MHZ
32 {
33 int start1, start2;
34 _asm rdtsc
35 _asm mov start1, eax
36 Sleep(50);
37 _asm rdtsc
38 _asm mov start2, eax
39 return ((start2 - start1) / 50) / (1024);
40 }
41
42 string GetManID()//获取制造商信息
43 {
44 char ID[25];//存储制造商信息
45 memset(ID, 0, sizeof(ID));//先清空数组 ID
46 ExeCPUID(0);//初始化
47 memcpy(ID + 0, &debx, 4);//制造商信息前四个字符复制到数组
48 memcpy(ID + 4, &dedx, 4);//中间四个
49 memcpy(ID + 8, &decx, 4);//最后四个
50 //如果返回 char * ,会出现乱码;故返回 string 值
51 return string(ID);
52 }
53
54 string GetCPUType()
55 {
56 const DWORD id = 0x80000002; //从0x80000002开始,到0x80000004结束
57 char CPUType[49];//用来存储CPU型号信息
58 memset(CPUType, 0, sizeof(CPUType));//初始化数组
59
60 for (DWORD t = 0; t < 3; t++)
61 {
62 ExeCPUID(id + t);
63 //每次循环结束,保存信息到数组
64 memcpy(CPUType + 16 * t + 0, &deax, 4);
65 memcpy(CPUType + 16 * t + 4, &debx, 4);
66 memcpy(CPUType + 16 * t + 8, &decx, 4);
67 memcpy(CPUType + 16 * t + 12, &dedx, 4);
68 }
69
70 return string(CPUType);
71 }
72
73 int main()
74 {
75 cout << "正在计算1e6*1e4次……" << endl;
76 clock_t start, end;
77 start = clock(); //程序开始计时
78 int ans = 0;
79 for (int i = 1; i <= 1e6; i++)
80 {
81 ans++;
82 for (int j = 0; j <= 1e4; j++)
83 {
84 ans++;
85 }
86 }
87 end = clock(); //程序结束用时
88 double endtime = (double)(end - start) / CLOCKS_PER_SEC;
89 cout << "Total time:" << endtime << "秒" << endl; //s为单位
90 cout << "Total time:" << endtime * 1000 << "毫秒" << endl; //ms为单位
91
92 cout << "本机CPU信息如下:" << endl;
93 cout << "CPU 主 频: " << GetCPUFreq() << " MHZ" << endl;
94 cout << "CPU 制造商: " << GetManID() << endl;
95 cout << "CPU 型 号: " << GetCPUType() << endl;
96 system("pause");
97 return 0;
98
99
100 }