cout输出数组_让程序从1开始一直执行++操作,10秒钟能输出最大的数是多少
問題描述
如果寫一段代碼,讓程序從 1 開始一直執(zhí)行 ++ 操作,在規(guī)定的 10s 鐘內(nèi),你能輸出的最大數(shù)是多少?并將它打印到屏幕上。
乍一看,你會覺得它是一道算法題,再細(xì)想:不對!這可能是一道性能題。
題目拆解
首先,定義一個變量 i,并賦值為 1, 接著進(jìn)入 while 循環(huán),執(zhí)行 i++. 需要注意的是,這個變量可能非常大,超出了類型的最大值,那么就需要用一個數(shù)組來存儲這個變量,數(shù)組中的每個元素分別表示這個數(shù)的每一位。
其次,單線程處理時,每次進(jìn)入 while 循環(huán)時,都需要獲取程序運(yùn)行時間,當(dāng)程序運(yùn)行時間快到 10s 時得趕緊退出循環(huán),并將這個“大數(shù)”打印到屏幕上。否則,如果沒來得及輸出到屏幕,就前功盡棄了。
進(jìn)階
看起來,上述思路已經(jīng)足以解決這個問題了。然而,這也許并不是最優(yōu)解。這里,我們采用多線程機(jī)制試試。具體步驟如下:
- 在主線程里開辟兩個新進(jìn)程 A 和 B;
- 在線程 A 里執(zhí)行 ++ 操作;
- 在線程 B 里實(shí)時獲取程序運(yùn)行時間;
- 在線程 A 里判斷是否運(yùn)行超時,并及時退出并輸出結(jié)果。
測試結(jié)果
如圖所示,intel 處理器 i5,4核。
硬件信息
測試結(jié)果
單線程時:10s 時間內(nèi) C++ 程序能夠輸出的最大值為 4819531;
多線程時:10s 時間內(nèi) C++ 程序能夠輸出的最大值為 19726951;
多線程性能是單線程性能的 4 倍!
原因分析
很可能是因?yàn)槎嗪?#xff0c;不同線程跑在不同的核上,充分利用 CPU。
軟件性能是每個程序員都要面臨的問題,從基本的代碼級性能優(yōu)化,到數(shù)據(jù) cache miss、指令 cache miss 優(yōu)化,再到多線程協(xié)同、綁核、資源調(diào)度算法,甚至對二進(jìn)制目標(biāo)文件的內(nèi)容進(jìn)行重排。。。等等,所涉及的面非常廣,也可以非常深。
附錄: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.h"int Global::bitNum = 64;int Global::expireTime = 10;ProcessInfo.h - 程序運(yùn)行信息類
#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.h"double 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總結(jié)
以上是生活随笔為你收集整理的cout输出数组_让程序从1开始一直执行++操作,10秒钟能输出最大的数是多少的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电力电子转战数字IC20220610da
- 下一篇: 8.1 文件查找local;find使用