Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include <chrono>
#include <string>
#include <iostream>
#include "CumBuffer.h"
typedef std::chrono::duration<int, std::milli> millisecs_t;
typedef std::chrono::duration<long long, std::micro> microsecs_t;
typedef enum _ENUM_TIME_RESOLUTION_
{
MILLI_SEC_RESOLUTION,
MICRO_SEC_RESOLUTION,
NO_RESOLUTION
} ENUM_TIME_RESOLUTION;
class ElapsedTime
{
public:
ElapsedTime()
{
time_resolution_ = NO_RESOLUTION;
SetStartTime();
}
ElapsedTime(std::string strDescription, ENUM_TIME_RESOLUTION time_resolution)
{
strDescription_ = strDescription;
time_resolution_ = time_resolution;
SetStartTime();
}
~ElapsedTime()
{
SetEndTime(time_resolution_);
}
void SetStartTime()
{
startT_ = std::chrono::steady_clock::now();
}
long long SetEndTime( ENUM_TIME_RESOLUTION resolution)
{
endT_ = std::chrono::steady_clock::now();
if(resolution == MILLI_SEC_RESOLUTION)
{
millisecs_t duration(std::chrono::duration_cast<millisecs_t>(endT_ - startT_));
if(time_resolution_ != NO_RESOLUTION)
{
std::cout << strDescription_ <<" elapsed : " << duration.count() << "(milli seconds)\n";
}
return duration.count();
}
else if (resolution == MICRO_SEC_RESOLUTION)
{
microsecs_t duration(std::chrono::duration_cast<microsecs_t>(endT_ - startT_));
if(time_resolution_ != NO_RESOLUTION)
{
std::cout <<strDescription_ << " elapsed : " << duration.count() << "(micro seconds)\n";
}
return duration.count();
}
return -1; //error
}
protected:
std::chrono::steady_clock::time_point startT_;
std::chrono::steady_clock::time_point endT_ ;
ENUM_TIME_RESOLUTION time_resolution_;
std::string strDescription_;
};
///////////////////////////////////////////////////////////////////////////////
// one packet = 1024
// 300 + 200 + 450 + 74 = 1024
char gData1 [300];
char gData2 [200];
char gData3 [450];
char gData4 [74];
char gDataOut [1024];
const int gMaxLoop = 1000000;
///////////////////////////////////////////////////////////////////////////////
bool TestDynamicAlloc()
{
ElapsedTime elapsed;
int nElapsedMilliSec= 0;
char* pTemp = NULL;
char* pBuffer = NULL;
int nBuffLength = 0;
int nChunkLen = 0;
elapsed.SetStartTime();
//------------
for(int i =0; i < gMaxLoop; i++)
{
for(int j =0; j < 4; j++)
{
if (j==0) { nChunkLen = 300; }
else if(j==1) { nChunkLen = 200; }
else if(j==2) { nChunkLen = 450; }
else if(j==3) { nChunkLen = 74; }
pTemp = pBuffer;
pBuffer = (char*)calloc(nBuffLength + nChunkLen, sizeof(char));
if (pBuffer == NULL)
{
return false;
}
if (nBuffLength != 0)
{
memcpy(pBuffer, pTemp, nBuffLength);
if (pTemp)
{
free(pTemp);
pTemp = NULL;
}
}
if (j==0) { memcpy(pBuffer + nBuffLength, gData1, nChunkLen); }
else if(j==1) { memcpy(pBuffer + nBuffLength, gData2, nChunkLen); }
else if(j==2) { memcpy(pBuffer + nBuffLength, gData3, nChunkLen); }
else if(j==3) { memcpy(pBuffer + nBuffLength, gData4, nChunkLen); }
nBuffLength = nBuffLength + nChunkLen;
} //for
memcpy (gDataOut, pBuffer, 1024);
//std::cout << "out = [" << gDataOut << "] \n";
//-------
if (pBuffer)
{
free(pBuffer);
pBuffer = NULL;
}
nBuffLength = 0;
} //for
//------------
nElapsedMilliSec= elapsed.SetEndTime(MILLI_SEC_RESOLUTION);
std::cout << " elapsed : "<< nElapsedMilliSec << "\n";
return true;
}
///////////////////////////////////////////////////////////////////////////////
bool TestCumBuffer()
{
ElapsedTime elapsed;
int nElapsedMilliSec= 0;
CumBuffer buffering;
if(cumbuffer_defines::OP_RSLT_OK != buffering.Init(1024*2))
{
return false;
}
elapsed.SetStartTime();
for(int i =0; i < gMaxLoop; i++)
{
//append 300 bytes
if(cumbuffer_defines::OP_RSLT_OK != buffering.Append(300, gData1 ))
{
return false;
}
//append 200 bytes
if(cumbuffer_defines::OP_RSLT_OK != buffering.Append(200, gData2))
{
return false;
}
//append 450 bytes
if(cumbuffer_defines::OP_RSLT_OK != buffering.Append(450, gData3 ))
{
return false;
}
//append 74 bytes
if(cumbuffer_defines::OP_RSLT_OK != buffering.Append(74, gData4))
{
return false;
}
//get 1024 bytes
if(cumbuffer_defines::OP_RSLT_OK != buffering.GetData(1024, gDataOut ))
{
return false;
}
}
//------------
nElapsedMilliSec= elapsed.SetEndTime(MILLI_SEC_RESOLUTION);
std::cout << " elapsed : "<< nElapsedMilliSec << "\n";
return true;
}
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
memset(gData1, 'a', sizeof(gData1));
memset(gData2, 'b', sizeof(gData2));
memset(gData3, 'c', sizeof(gData3));
memset(gData4, 'd', sizeof(gData4));
std::cout << "\n";
std::cout << "* 1024 bytes Dynamic Alloc (milli sec)\n";
TestDynamicAlloc();
TestDynamicAlloc();
TestDynamicAlloc();
std::cout << "\n";
std::cout << "* 1024 bytes CumBuffer (milli sec)\n";
TestCumBuffer();
TestCumBuffer();
TestCumBuffer();
std::cout << "\n\n";
return 0;
}
// g++ -O2 --std=c++11 -o cumbench cumbench.cpp
/*
kojh-mb-pro:benchmark kojunghyun$ ./benchmark
* 1024 bytes Dynamic Alloc (milli sec)
elapsed : 669
elapsed : 664
elapsed : 658
* 1024 bytes CumBuffer (milli sec)
elapsed : 94
elapsed : 96
elapsed : 92
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment