1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
template<typename unaryOP>
double timeCount(unaryOP op, size_t const LOOP = 1, char const* NAME = "function")
{
std::clock_t begin, end;
begin = std::clock();
for(size_t i = 0; i != LOOP; ++i) op();
end = std::clock();
double const RESULT = (end - begin) / static_cast<double>(CLOCKS_PER_SEC);
size_t const HOUR = static_cast<size_t>(RESULT / 3600);
size_t const MIN = static_cast<size_t>(RESULT) / 60 - HOUR * 60;
double const SEC = RESULT - static_cast<double>(MIN * 60 + HOUR * 3600);
std::cout<<"time of the(hours : min : sec) "<<NAME<<" : "<<HOUR<<" : "<<MIN<<" : "<<SEC;
std::cout<<"\nloop "<<LOOP<<" times\n";
return RESULT;
}
void* CFind3(void* begin, void* end, int const VALUE)
{
while(begin != end)
{
if(*(int*)begin == VALUE) break;
begin = (char*)begin + sizeof(int);
}
return begin;
}
void* CFind4(void *begin, size_t const NUM, int const VALUE)
{
size_t i = 0;
for(; i != NUM; ++i)
{
if(*(int*)begin == VALUE) break;
begin = (char*)(begin) + sizeof(int);
}
return begin;
}
void testCFind3()
{
int data[] = {1, 20, 33, 55, 22, 77, 88, 99, 3000, 8880, 800, 777, 345, 976, 345, 222, 111, 0, 77, 88, 99, 1, 2, 3, 20000};
size_t const NUM = static_cast<size_t>(1E6);
size_t i = 0;
for(i = 0; i != NUM; ++i) void *temp = CFind3(&data[0], &data[0] + sizeof(data) / sizeof(*data), 20000);
}
void testCFind4()
{
int data[] = {1, 20, 33, 55, 22, 77, 88, 99, 3000, 8880, 800, 777, 345, 976, 345, 222, 111, 0, 77, 88, 99, 1, 2, 3, 20000};
size_t const NUM = static_cast<size_t>(1E6);
size_t i = 0;
for(i = 0; i != NUM; ++i) void *temp = CFind4(&data[0], sizeof(data) / sizeof(*data), 20000);
}
int main()
{
timeCount(testCFind3, static_cast<size_t>(1E2), "testCFind3");
timeCount(testCFind4, static_cast<size_t>(1E2), "testCFind4");
std::cout<<"system pause"<<std::endl;
std::cin.get();
return 0;
}
|