calculating execution time in c++

后端 未结 6 551
庸人自扰
庸人自扰 2020-12-22 19:26

I have written a c++ program , I want to know how to calculate the time taken for execution so I won\'t exceed the time limit.

#include

usin         


        
6条回答
  •  自闭症患者
    2020-12-22 19:58

    OVERVIEW

    I have written a simple semantic hack for this using @AshutoshMehraresponse. You code looks really readable this way!

    MACRO

    #include 
    
    #ifndef SYSOUT_F
    #define SYSOUT_F(f, ...)      _RPT1( 0, f, __VA_ARGS__ ) // For Visual studio
    #endif
    
    #ifndef speedtest__             
    #define speedtest__(data)   for (long blockTime = NULL; (blockTime == NULL ? (blockTime = clock()) != NULL : false); SYSOUT_F(data "%.9fs", (double) (clock() - blockTime) / CLOCKS_PER_SEC))
    #endif
    

    USAGE

    speedtest__("Block Speed: ")
    {
        // The code goes here
    }
    

    OUTPUT

    Block Speed: 0.127000000s
    

提交回复
热议问题