构造优化

    技术2025-01-05  47

    // CmdLine.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" const int kMillion = 1000; class Base { public: Base(int id) { id_ = 2; } void compute() { id_ *= 400; } private: int id_; }; class Derived: public Base { public: Derived(int id):Base(id) {} }; #include "tick_counter.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { TickCounter counter1; for (int i = 0; i < 10; ++i) { counter1.start(); for (int i = 0; i < kMillion; ++i) { Derived child(1); child.compute(); } unsigned __int64 time1 = counter1.stop(); cout << time1 << endl; TickCounter counter2; counter2.start(); for (int i = 0; i < kMillion; ++i) { Base child(1); child.compute(); //i = i +2; } unsigned __int64 time2 = counter2.stop(); cout << time2 << endl; cout << "==========================" << endl; } getchar(); return 0; }  

    Debug版效率有50%提升。Release 版执行速度无太大差别。VC编译器的优化相当彻底。

    最新回复(0)