cout输出数组_让程序从1开始一直执行++操作,10秒钟能输出最大的数是多少
問題描述
如果寫一段代碼,讓程序從 1 開始一直執行 ++ 操作,在規定的 10s 鐘內,你能輸出的最大數是多少?并將它打印到屏幕上。
乍一看,你會覺得它是一道算法題,再細想:不對!這可能是一道性能題。
題目拆解
首先,定義一個變量 i,并賦值為 1, 接著進入 while 循環,執行 i++. 需要注意的是,這個變量可能非常大,超出了類型的最大值,那么就需要用一個數組來存儲這個變量,數組中的每個元素分別表示這個數的每一位。
其次,單線程處理時,每次進入 while 循環時,都需要獲取程序運行時間,當程序運行時間快到 10s 時得趕緊退出循環,并將這個“大數”打印到屏幕上。否則,如果沒來得及輸出到屏幕,就前功盡棄了。
進階
看起來,上述思路已經足以解決這個問題了。然而,這也許并不是最優解。這里,我們采用多線程機制試試。具體步驟如下:
- 在主線程里開辟兩個新進程 A 和 B;
- 在線程 A 里執行 ++ 操作;
- 在線程 B 里實時獲取程序運行時間;
- 在線程 A 里判斷是否運行超時,并及時退出并輸出結果。
測試結果
如圖所示,intel 處理器 i5,4核。
硬件信息
測試結果
單線程時: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.h"int 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.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總結
以上是生活随笔為你收集整理的cout输出数组_让程序从1开始一直执行++操作,10秒钟能输出最大的数是多少的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电力电子转战数字IC20220610da
- 下一篇: 8.1 文件查找local;find使用