一直听人说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()函数真实性能分析相关推荐

  1. C语言:srand函数与rand函数的使用(纯干货)【易懂】

    C语言:srand函数与rand函数的使用(纯干货)[易懂] 文章目录 C语言:srand函数与rand函数的使用(纯干货)[易懂] 一.rand() 二.srand() 三.time() 四.运用: ...

  2. 【C语言】随机数函数rand和srand

    文章目录 一.随机数函数 1.rand().srand() 2.time() 二.案例实现 1.案例描述 2.代码实现 一.随机数函数 1.rand().srand() C语言产生随机数要用到的函数是 ...

  3. C语言学习笔记07-2-循环的游戏实验:猜数字(附rand、srand函数说明)

    C语言猜数字游戏实验(07-1笔记的补充) 在C/C++编程练习中代码太多,不能明显体现有关分支.循环语句结构的实验变化,在此处单独归纳汇总一份我尝试过的三次猜数字游戏设计. 猜数字初版 #inclu ...

  4. rand在c语言那一个函数库,浅析C语言中的rand函数和srand函数(一)

    我们在编程实现算法的过程中,往往需要使用到随机数.由于计算机是一台以逻辑为基础的机器,没法做到真正的随机(大概量子计算机可以?).所以计算机生成的是伪随机数,供我们使用. 我们使用C语言的rand函数 ...

  5. C语言中随机数的生成(rand函数和srand函数)

    CSDN话题挑战赛第2期 参赛话题:学习笔记 学习之路,长路漫漫,写学习笔记的过程就是把知识讲给自己听的过程.这个过程中,我们去记录思考的过程,便于日后复习,梳理自己的思路.学习之乐,独乐乐,不如众乐 ...

  6. C语言详解生成随机数的过程,time函数、时间戳timer、rand函数和srand函数,附猜数字小游戏

    第十一篇:随机数详解 一.准备工作(预备知识) 1.1.生成伪随机数(函数rand) 1.2.伪随机数"变成"随机数(函数time) 1.3.生成确定范围随机数 二.练手随机数经典 ...

  7. C语言之rand()和srand()函数

    1.rand().srand()函数介绍 srand 初始化随机种子,rand 产生随机数 定义函数:int rand(void) 函数功能:产生随机数 函数说明:rand的内部实现是用线性同余法做的 ...

  8. C语言 rand函数,srand函数 生成随机数用法详解

    与随机数有关的两个函数,srand() 和 rand(),这两个函数都包含"stdlib.h"头文件里 srand()函数是随机数发生器,使得每轮产生的随机数列都不相同.      ...

  9. C语言rand和srand函数的简单介绍和用法

    目录 介绍: 用法: 介绍: 在c语言中,我们想要在一个范围内随机生成一个数字,我们就需要用到rand函数,生成大小为0到32767的整数,但仅靠rand生成的数是伪随机的数,如下: 第一次运行n次: ...

最新文章

  1. C#调用win32 api程序实例
  2. python3多线程----锁机制
  3. Symantec Endpoint Protection 11 混乱的版本
  4. jQuery 分页插件 jPages 使用
  5. 在alv grid中的列中设置icon图标
  6. 上帝造题的七分钟(ybtoj-树状数组)
  7. 京信通信:数据智能为生产调试“增效瘦身”
  8. 何恺明随机连接神经网络复现
  9. CCIE-交换路由复习笔记
  10. English trip -- VC(情景课)1 F Another view
  11. mysql命令更新数据库_命令操作MySQL数据库
  12. intel wifi 5100agn linux驱动,intel5100agn网卡驱动下载
  13. DDNS动态域名解析
  14. java立方根怎么打_计算机上怎么打立方根
  15. 【概率论】事件的独立与事件的互斥(或互不相容)、以及它们之间的关系
  16. Trying to start MapKit location updates without prompting for location authorization. Must call -[CL
  17. 飞速创新更新IPO招股书:计划募资约14亿元,向伟为实际控制人
  18. threejs 实现小飞机建模
  19. 创建型模式之抽象工厂(AbstractFactory)
  20. 0056-在OpenCV环境下使用混合高斯背景建模提取前景目标

热门文章

  1. 为DedeCms的RSS生成绝对地址
  2. Response.Redirect 与 异常(线程正在中止)
  3. 系统分析师考试结束了
  4. 中石油训练赛 - Historical Maths(二分)
  5. 2021已去,2022未来
  6. easyui是否容易上手_特色家常菜-清蒸桂鱼,肉质鲜嫩有营养,做法简单容易学...
  7. 《Windows核心编程》---Interlocked原子访问系列函数
  8. cocos2d-x游戏实例(6)-A星算法(2)
  9. 网卡MAC地址相关信息大全
  10. 端口复用突破防火墙(图)