linux下使用C语言如何获得CPU的主频
http://blog.csdn.net/jesun/archive/2008/04/05/2252679.aspx#include#includeint main(){ FILE *fp; char str[81]; memset(str,0,81); fp=popen("cat /proc/cpuinfo|
·
http://blog.csdn.net/jesun/archive/2008/04/05/2252679.aspx
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *fp;
char str[81];
memset(str,0,81);
fp=popen("cat /proc/cpuinfo|grep cpu\\ MHz|sed -e 's/.*:[^0-9]//'","r");
if(fp<0){
printf("无法读取 CPU主频信息\n");
exit(1);
}
fgets(str,80,fp);
fclose(fp);
double spd=atof(str);
printf("您 CPU的 主频是%fMHz\n",spd);
exit(0);
}
#include <stdio.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/time.h>
static int64_t rdtsc(void)
{
unsigned int i, j;
asm volatile (" rdtsc" : "=a"(i), "=d"(j) : );
return ((int64_t)j<<32) + (int64_t)i;
}
int main()
{
int64_t tsc_start, tsc_end;
struct timeval tv_start, tv_end;
int usec_delay;
tsc_start = rdtsc();
gettimeofday(&tv_start, NULL);
usleep(100000);
tsc_end = rdtsc();
gettimeofday(&tv_end, NULL);
usec_delay = 1000000 * (tv_end.tv_sec - tv_start.tv_sec)
+ (tv_end.tv_usec - tv_start.tv_usec);
printf(" cpu MHz\t\t: %.3f\n",
(double)(tsc_end-tsc_start) / usec_delay);
}
cat /proc/cpuinf | awk -F: 'BEGIN{printf " cpu frequency is :"}{if($1~/ cpu /)print $2}'
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *fp;
char str[81];
memset(str,0,81);
fp=popen("cat /proc/cpuinfo|grep cpu\\ MHz|sed -e 's/.*:[^0-9]//'","r");
if(fp<0){
printf("无法读取 CPU主频信息\n");
exit(1);
}
fgets(str,80,fp);
fclose(fp);
double spd=atof(str);
printf("您 CPU的 主频是%fMHz\n",spd);
exit(0);
}
#include <stdio.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/time.h>
static int64_t rdtsc(void)
{
unsigned int i, j;
asm volatile (" rdtsc" : "=a"(i), "=d"(j) : );
return ((int64_t)j<<32) + (int64_t)i;
}
int main()
{
int64_t tsc_start, tsc_end;
struct timeval tv_start, tv_end;
int usec_delay;
tsc_start = rdtsc();
gettimeofday(&tv_start, NULL);
usleep(100000);
tsc_end = rdtsc();
gettimeofday(&tv_end, NULL);
usec_delay = 1000000 * (tv_end.tv_sec - tv_start.tv_sec)
+ (tv_end.tv_usec - tv_start.tv_usec);
printf(" cpu MHz\t\t: %.3f\n",
(double)(tsc_end-tsc_start) / usec_delay);
}
cat /proc/cpuinf | awk -F: 'BEGIN{printf " cpu frequency is :"}{if($1~/ cpu /)print $2}'
更多推荐
已为社区贡献2条内容
所有评论(0)