x86計算CPU頻率示例

#include <stdio.h>                                
#include <stdlib.h>                               
#include <assert.h>                               
#include <stdint.h>                               
#include <string.h>                               
#include <time.h>                                 
#include <sys/types.h>                            
#include <sys/stat.h>                             
#include <fcntl.h>                                
#include <unistd.h>                               
#include <ctype.h>                                
                                                   
#define ns 1000000000                             
                                                  
                                                   
//  Linux/GCC                                     
                                                   
static inline uint64_t get_cycle_count()          
{                                                 
    unsigned int lo,hi;                           
    __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
    return ((uint64_t)hi << 32) | lo;             
}                                                 
                                                   
int main(int argc,char ** argv)                   
{                                                 
                                                   
    int i = 0;                                    
    uint64_t start,end;                             
    int ms = 700;                            
    for(i = 0; i < 100; i++)                      
    {                                             
        start = get_cycle_count();                  
        usleep(ms * 1000);                   
        end = get_cycle_count();                  
        printf("%u\n",(new-old)/(ms*1000));                                                                                                                                       
                                                   
    }                                             
}
發佈了101 篇原創文章 · 獲贊 84 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章