添加测试时间的代码:
#include<iostream>
#include<memory>
#include<chrono>
class Timer {
public:
Timer() {//Timer构建时开始计时
m_StartTimePoint = std::chrono::high_resolution_clock::now();
}
~Timer() {//Timer销毁时,停止计时 ---->RAII 超出范围对象就被解析
Stop();
}
void Stop() {
auto endTimePoint = std::chrono::high_resolution_clock::now();
auto start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimePoint).time_since_epoch().count();
auto end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimePoint).time_since_epoch().count();
auto duration = end - start;
double ms = duration * 0.001;
std::cout << "duration :" << duration << " us (" << duration * 0.001 << " ms)" << std::endl;
}
private:
std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimePoint;
};
int main() {
int value = 0;
{ //增加一个作用域,结束的时候会Timer会启动计算Stop()
Timer timer;
for (int i = 0; i < 1000000; i++) {
value += 2;
}
std::cout << value << std::endl;
}
__debugbreak();
return 0;
}
库函数 #include<chrono,这是一个用于与时间相关的一个库
chrono是标题的名称,也是子命名空间的名称:该标头中的所有元素(common_type特殊化
除外)不是直接在std命名空间下定义(像大多数标准库一样),而是在std ::下定义:
chrono名称空间。
此标头中的元素处理时间。这主要通过三个概念来完成:
持续时间
它们测量时间跨度,例如:1分钟,2小时或10毫秒。
在此库中,它们用持续时间类模板的对象表示,该对象将计数表示形式和周期精度结合在一起(例如,十毫秒的conds具有十作为计数表示形式,毫秒作为周期精度)。
时间点
对特定时间点的参考,例如生日,今天的黎明或下一趟火车何时通过。
在此库中,time_point类模板的对象通过使用相对于纪元的持续时间(这是使用同一时钟的所有time_point对象共有的固定时间点)来表示这一点。
钟表
将时间点与实际物理时间相关联的框架。
该库提供至少三个时钟,其提供的方法来表达所述当前时间作为time_point:SYSTEM_CLOCK,steady_clock和high_resolution_clock。
小技巧:
__debugbreak()方法可以让我们对程序添加断点进行调试
debug 模式 3.697ms
for (int i = 0; i < 1000000; i++) {
00133460 mov dword ptr [ebp-34h],0
00133467 jmp main+72h (0133472h)
00133469 mov eax,dword ptr [ebp-34h]
0013346C add eax,1
0013346F mov dword ptr [ebp-34h],eax
00133472 cmp dword ptr [ebp-34h],0F4240h
00133479 jge main+86h (0133486h)
value += 2;
0013347B mov eax,dword ptr [value]
0013347E add eax,2
00133481 mov dword ptr [value],eax
}
00133484 jmp main+69h (0133469h)
std::cout << value << std::endl;
00133486 mov esi,esp
00133488 push offset std::endl<char,std::char_traits<char> > (0131325h)
0013348D mov edi,esp
0013348F mov eax,dword ptr [value]
00133492 push eax
00133493 mov ecx,dword ptr [_imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A (013E0E8h)]
00133499 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (013E0B0h)]
0013349F cmp edi,esp
001334A1 call __RTC_CheckEsp (01312FDh)
001334A6 mov ecx,eax
001334A8 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (013E0B4h)]
001334AE cmp esi,esp
001334B0 call __RTC_CheckEsp (01312FDh)
}
001334B5 mov dword ptr [ebp-4],0FFFFFFFFh
001334BC lea ecx,[ebp-28h]
001334BF call Timer::~Timer (01310EBh)
release模式
{
Timer timer;
00DA1215 lea ecx,[timer]
00DA1218 call Timer::Timer (0DA1000h)
for (int i = 0; i < 1000000; i++) {
value += 2;
}
std::cout << value << std::endl;
00DA121D push offset std::endl<char,std::char_traits<char> > (0DA1580h)
00DA1222 mov dword ptr [ebp-4],0
00DA1229 mov ecx,dword ptr [_imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A (0DA3068h)]
00DA122F push 1E8480h
00DA1234 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0DA3038h)]
00DA123A mov ecx,eax
00DA123C call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0DA3034h)]
}
00DA1242 lea ecx,[timer]
00DA1245 mov dword ptr [ebp-4],1
00DA124C call Timer::Stop (0DA10D0h)
汇编代码中,ecx,eax是寄存器。
push和pop是用来操作栈的2个指令。
push寄存器:将一个寄存器中的数据入栈
pop寄存器:出栈用一个寄存器接收数据
16位处理器有14个寄存器,每个寄存器有各自的名字。
14个寄存器:AX、BX、CX、DX、SI、DI、SP、BP、IP、CS、SS、DS、ES、PSW。
32位处理器有16个寄存器,每个寄存器有各自的名字。
16个寄存器:EAX、EBX、ECX、EDX、ESI、EDI、ESP、EBP、ES、CS、SS、DS、FS、GS、EIP、EFLAGS。
64位处理器有16个寄存器,每个寄存器有各自的名字。
16个寄存器:RAX、RBX、RCX、RDX、RSI、RDI、RBP、RSP、CS、DS、ES、SS、FS、GS、RIP、RFLAGS。
推荐博客:https://blog.csdn.net/weixin_40913261/article/details/90762210