Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Perf measurement
//vector test #include <iostream> #include <vector> #ifdef PLATFORM_WINDOWS #include <Windows.h> #else #include <sys/time.h> #include <time.h> #endif class PerfCounter { private: #ifdef PLATFORM_WINDOWS int64_t m_QueryPerfFrequency; int64_t m_startTime; #else timespec m_startTime; #endif public: PerfCounter(); void startTime(); float getTime(); //in milliseconds. }; PerfCounter::PerfCounter() { #ifdef PLATFORM_WINDOWS QueryPerformanceFrequency((LARGE_INTEGER *)&m_QueryPerfFrequency); #endif } void PerfCounter::startTime() { #ifdef PLATFORM_WINDOWS QueryPerformanceCounter((LARGE_INTEGER *)&m_startTime); #else clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &m_startTime); #endif } float PerfCounter::getTime() { float time = 0; #ifdef PLATFORM_WINDOWS LONGLONG endTime = 0; QueryPerformanceCounter((LARGE_INTEGER *)&endTime); LONGLONG diff = endTime - m_startTime; time = (diff * 1.0f / m_QueryPerfFrequency) * 1000; #else timespec endTime; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &endTime); time = (endTime.tv_sec - m_startTime.tv_sec) * 1000; //in milliseconds time += (endTime.tv_nsec - m_startTime.tv_nsec) * 1.0f / 1000000; // in microseconds #endif return time; } #define POWOF2(x) (1 << x) std::vector<uint8_t> convertToBinary(std::vector<bool> &buf) { std::vector<uint8_t> retVal(buf.size() / 8 + 1); uint8_t byte = 0; for(uint32_t i = 0; i < buf.size();) { byte = 0; for(uint8_t j = 0; j < 8 && (i + j) < buf.size(); j++) { if(buf.at(i)) { byte += POWOF2(j); } } retVal.push_back(byte); i += 8; } return retVal; } int main() { bool arr[1000000]; std::vector<bool> vec; bool b = false; PerfCounter p; p.startTime(); for(int i = 0; i < 1000000; i++) arr[i] = b; std::cout<<"Array. time = "<<p.getTime()<<std::endl; p.startTime(); for(int i = 0; i < 1000000; i++) vec.push_back(b); std::cout<<"Vector. time = "<<p.getTime()<<std::endl; std::cout << "Hello, world!\n"; p.startTime(); std::vector<bool> v; v.reserve(1000000); for(int i = 0; i < v.size(); i++) v[i]=true; std::cout<<"vector conversion time = "<<p.getTime()<<std::endl; p.startTime(); for(int i = 0; i < 1000000; i++) { int *iPtr = new int(); delete iPtr; } std::cout<<"new time = "<<p.getTime()<<std::endl; p.startTime(); int *ptr = new int(); for(int i = 0; i < 1000000; i++) { int *iPtr = new (ptr) int(); ::operator delete(ptr, iPtr); } std::cout<<"new time = "<<p.getTime()<<std::endl; }
run
|
edit
|
history
|
help
0
Prime Factor
UsingAlgorithm
shared_ptr deleter
Silly circular pointer
P1
Dar
constexpr expirements
HeapSort
Varadic macro
test if nullptr