當前位置:
首頁 >
C语言rand(),srand()函数真实性能分析
發布時間:2024/2/28
34
豆豆
生活随笔
收集整理的這篇文章主要介紹了
C语言rand(),srand()函数真实性能分析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一直聽人說c標準庫的rand(),?random()隨機數產生函數性能極差。一直信以為真,今天做實驗,發現并非如此
實驗結論如下:
1. 系統自帶的rand()和random()函數性能極高,大約相當于2.5次i++
2. rand()函數比random()函數性能稍差,差距大約在10%左右
3. Srand()函數性能非常差,大約比random()函數差了170倍左右,也就是約等于425次i++
4.?rand的實現就是簡單的乘法和取模,自己實現的隨機數在性能上幾乎無法超越系統自帶的
5. 微軟實現的隨機數rand()很高效,srand()函數也很高效
實驗結果如下圖:
測試代碼如下:
#include <stdio.h> #include <stdint.h> #include <vector> #include <algorithm> #include <stdlib.h> #include <math.h> #include <sys/time.h> #include<iostream> using namespace std;class Random {public:static void srandom(int randSeedNum = 321);static int random(); private:static bool m_bInit; // 是否已初始化完成static int m_count; // 計數器static vector<int> m_randSeeds;// 存放隨機種子 };bool Random::m_bInit = false; int Random::m_count = 0; vector<int> Random::m_randSeeds;// 設置隨機種子 void Random::srandom(int randSeedNum) {// 先清空m_randSeeds.clear();// 再壓入0,1,2,3 .... randSeedNum-2for(int i=0; i< randSeedNum; ++i){m_randSeeds.push_back( i );}// 打亂std::random_shuffle(m_randSeeds.begin(), m_randSeeds.end());// 標記已初始化完成m_bInit = true; }// 返回一個隨機數 int Random::random() {// 未初始化,則先初始化if(!m_bInit){srandom();}// 隨機種子的vector長度static int size = m_randSeeds.size();// 每次自增后,得到隨機數return 16777619 * m_randSeeds[m_count++%size]; }// 微軟的rand實現(POSIX.1-2001的樣例) static unsigned int next_start = 1; int randMicro(void) {next_start = next_start * 1103515245 + 12345;/* return (unsigned int)(next_start / 65536) % 32768;*/return (int)(next_start>>16) & RAND_MAX; } void srandMicro(unsigned int seed) {/* And you *should* get a warning if sizes dont match*/next_start = seed; }// 變量自增函數 int Inc() {static int iCount = 0;return iCount++; }// 返回時間間隔,單位:毫秒 int GetPastTime(timeval* pStartTv) {timeval endTv;gettimeofday(&endTv, NULL);int uSecond = ((endTv.tv_sec - pStartTv->tv_sec) * 1000000 + endTv.tv_usec - pStartTv->tv_usec)/1000;return uSecond; }// i++函數1 void TestInc1(int count) {// 起始時間timeval startTv;gettimeofday(&startTv, NULL);// 自增count次for(int i=0; i<count; ++i){Inc();}// 結束時間int past = GetPastTime(&startTv);cout<<"i++函數1 執行次數:"<<count<<" 消耗時間:"<<past<<"ms"<<endl; }// i++函數2 void TestInc2(int count) {// 起始時間timeval startTv;gettimeofday(&startTv, NULL);static int icount = 0;for(int i=0; i<count; ++i){icount ++;}// 結束時間int past = GetPastTime(&startTv);cout<<"i++函數2 執行次數:"<<count<<" 消耗時間:"<<past<<"ms"<<endl; }// 自實現隨機函數 void TestMyRandom(int count) {// 隨機種子,不計時Random::srandom();// 起始時間timeval startTv;gettimeofday(&startTv, NULL);for(int i=0; i<count; ++i){Random::random();}// 結束時間int past = GetPastTime(&startTv);cout<<"自實現隨機函數1 執行次數:"<<count<<" 消耗時間:"<<past<<"ms"<<endl; }// 微軟實現的隨機數 void TestMicroRandom(int count) {// 起始時間timeval startTv;gettimeofday(&startTv, NULL);for(int i=0; i<count; ++i){randMicro();}// 結束時間int past = GetPastTime(&startTv);cout<<"微軟實現的隨機數 執行次數:"<<count<<" 消耗時間:"<<past<<"ms"<<endl; }// 系統函數random void TestRandom(int count) { // 起始時間timeval startTv;gettimeofday(&startTv, NULL);for(int i=0; i<count; ++i){random();}// 結束時間int past = GetPastTime(&startTv);cout<<"系統函數random 執行次數:"<<count<<" 消耗時間:"<<past<<"ms"<<endl; }// 系統函數rand void TestRand(int count) { // 起始時間timeval startTv;gettimeofday(&startTv, NULL);for(int i=0; i<count; ++i){rand();}// 結束時間int past = GetPastTime(&startTv);cout<<"系統函數rand 執行次數:"<<count<<" 消耗時間:"<<past<<"ms"<<endl; }// 系統函數srand void TestSrand(int count) { // 起始時間timeval startTv;gettimeofday(&startTv, NULL);for(int i=0; i<count; ++i){srand(i);}// 結束時間int past = GetPastTime(&startTv);cout<<"系統函數srand 執行次數:"<<count<<" 消耗時間:"<<past<<"ms"<<endl; }int main(int argc, char** argv) {// 執行1億次int count = 100000000;cout<<"請選擇要測試的函數:"<<endl;cout<<"1: 調用函數,靜態變量自增"<<endl;cout<<"2: 靜態變量自增"<<endl;cout<<"3: 自實現隨機函數"<<endl;cout<<"4: 微軟實現的隨機數"<<endl;cout<<"5: 系統函數random()"<<endl;cout<<"6: 系統函數rand()"<<endl;cout<<"7: 系統函數srand()"<<endl;cout<<"請輸入:";int choise = 1;cin>>choise;switch(choise){case 1: // i++函數1TestInc1(count);break;case 2: // i++函數2TestInc2(count);break;case 3: // 系統函數randomTestMyRandom(count);break;case 4: // 微軟實現的隨機數TestMicroRandom(count);break;case 5: // 系統函數random()TestRandom(count);break;case 6: // 系統函數rand()TestRand(count);break;case 7: // 系統函數srand() TestSrand(count);break;default:cout<<"錯誤的類型"<<endl;}return 0; }?
總結
以上是生活随笔為你收集整理的C语言rand(),srand()函数真实性能分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kafka史上最详细总结
- 下一篇: 网络抓包工具 wireshark教程