手机大型网站,网站开发离线下载报表,我请网络公司做的网站上的图片被当广告拦截了_怎么回事,温岭新站seo问题描述如果写一段代码#xff0c;让程序从 1 开始一直执行 操作#xff0c;在规定的 10s 钟内#xff0c;你能输出的最大数是多少#xff1f;并将它打印到屏幕上。乍一看#xff0c;你会觉得它是一道算法题#xff0c;再细想#xff1a;不对#xff01;这可能是一道…问题描述如果写一段代码让程序从 1 开始一直执行 操作在规定的 10s 钟内你能输出的最大数是多少并将它打印到屏幕上。乍一看你会觉得它是一道算法题再细想不对这可能是一道性能题。题目拆解首先定义一个变量 i并赋值为 1, 接着进入 while 循环执行 i. 需要注意的是这个变量可能非常大超出了类型的最大值那么就需要用一个数组来存储这个变量数组中的每个元素分别表示这个数的每一位。其次单线程处理时每次进入 while 循环时都需要获取程序运行时间当程序运行时间快到 10s 时得赶紧退出循环并将这个“大数”打印到屏幕上。否则如果没来得及输出到屏幕就前功尽弃了。进阶看起来上述思路已经足以解决这个问题了。然而这也许并不是最优解。这里我们采用多线程机制试试。具体步骤如下在主线程里开辟两个新进程 A 和 B在线程 A 里执行 操作在线程 B 里实时获取程序运行时间在线程 A 里判断是否运行超时并及时退出并输出结果。测试结果硬件信息如图所示intel 处理器 i54核。硬件信息性能数据测试结果单线程时10s 时间内 C 程序能够输出的最大值为 4819531多线程时10s 时间内 C 程序能够输出的最大值为 19726951多线程性能是单线程性能的 4 倍原因分析很可能是因为多核不同线程跑在不同的核上充分利用 CPU。软件性能是每个程序员都要面临的问题从基本的代码级性能优化到数据 cache miss、指令 cache miss 优化再到多线程协同、绑核、资源调度算法甚至对二进制目标文件的内容进行重排。。。等等所涉及的面非常广也可以非常深。附录C 代码供参考main.cpp#include Global.h#include ProcessInfo.h#include Test.h#include #include #include namespace Single {void SinglePrint(std::vector val){ std::cout testVal is: ; unsigned int i 0; for (; i val.size(); i) { if (val[i] ! 0) { break; } } for (unsigned int j i; j val.size(); j) { std::cout val[j]; } std::cout std::endl;}void SingleTestAdd(){ std::vector testVal(Global::bitNum, 0); while (true) { int flag 0; int tmp; for (int i Global::bitNum - 1; i 0; i--) { if (i Global::bitNum - 1) { tmp testVal[i] 1 flag; } else { tmp testVal[i] flag; } if (tmp 10) { flag 1; } else { flag 0; } testVal[i] (tmp % 10); } clock_t t clock(); ProcessInfo::currTime (double)t / CLOCKS_PER_SEC; if ((ProcessInfo::currTime - ProcessInfo::startTime) Global::expireTime) { std::cout currTime: ProcessInfo::currTime std::endl; SinglePrint(testVal); break; } } return;};}int main(){ std::cout test multi thread performance : std::endl; ProcessInfo::GetProcessStartTime(); std::cout startTime: ProcessInfo::startTime std::endl; std::thread t1(ProcessInfo::GetProcessCurrTime); std::thread t2(Test::TestAdd); t1.detach(); t2.join(); std::cout test single thread performance : std::endl; ProcessInfo::GetProcessStartTime(); std::cout startTime: ProcessInfo::startTime std::endl; Single::SingleTestAdd(); return 0;}Global.h - 单例类定义全局变量#ifndef _GLOBAL_H_#define _GLOBAL_H_#include class Global {private: Global(){}; static Global* global;public: static Global* GetGlobalInstance() { if (global NULL) { global new Global(); } return global; }public: static int bitNum; static int expireTime;};#endifGlobal.cpp#include Global.hint Global::bitNum 64;int Global::expireTime 10;ProcessInfo.h - 程序运行信息类#ifndef _PROCESSINFO_H_#define _PROCESSINFO_H_#include class ProcessInfo{public: ProcessInfo(){}; static double startTime; static double currTime; static void GetProcessStartTime() { clock_t tmp clock(); startTime (double)tmp / CLOCKS_PER_SEC; }; static void GetProcessCurrTime() { while(1) { clock_t tmp clock(); currTime (double)tmp / CLOCKS_PER_SEC; } };};#endifProcessInfo.cpp#include ProcessInfo.hdouble ProcessInfo::startTime 0;double ProcessInfo::currTime 0;Test.h - 测试类#ifndef _TEST_H_#define _TEST_H_#include ../module1/ProcessInfo.h#include ../module1/Global.h#include #include #include class Test{public: Test(){}; static void TestAdd() { std::vector testVal(Global::bitNum, 0); while (true) { int flag 0; int tmp; for (int i Global::bitNum - 1; i 0; i--) { if (i Global::bitNum - 1) { tmp testVal[i] 1 flag; } else { tmp testVal[i] flag; } if (tmp 10) { flag 1; } else { flag 0; } testVal[i] (tmp % 10); } if ((ProcessInfo::currTime - ProcessInfo::startTime) Global::expireTime) { std::cout currTime: ProcessInfo::currTime std::endl; PrintTestVal(testVal); break; } } return; }; static void PrintTestVal(std::vector val) { std::cout testVal is: ; unsigned int i 0; for (; i val.size(); i) { if (val[i] ! 0) { break; } } for (unsigned int j i; j val.size(); j) { std::cout val[j]; } std::cout std::endl; }};#endif