Autotimer. h
# Include <boost/enable_shared_from_this.hpp> # include <String>Using NamespaceSTD;ClassAutotimer:PublicPimpl <autotimer>: pointer_semantics,PublicBoost: enable_shared_from_this <autotimer> {Public: Autotimer (ConstSTD ::String& Name );~ Autotimer (Void);};
Autotimer. cpp
# Include" Autotimer. h "# Include <iostream> # include <windows. h> # include < String > Template <>Struct Pimpl <autotimer >:: implementation {implementation ( String K): mname (k) {mstarttime = gettickcount ();} Double Getelapsed () Const ; String Mname; DWORD mstarttime ;}; template <> Double Pimpl <autotimer >:: implementation: getelapsed () Const { Return (Gettickcount ()-mstarttime)/1e3;} autotimer: autotimer ( Const STD :: String & Name ):Base (Name) {} autotimer ::~ Autotimer ( Void ) {Implementation Const & Self = ** This ; STD: cout <self. mname <" : Took "<Self. getelapsed () <" Secs "<STD: Endl ;}Main. cpp
int _ tmain ( int argc, _ tchar * argv []) {autotimer timer (" mytimer "); for ( int I = 0; I <1000; ++ I) {cout <". ";}cout
return 0 ;}