taming-cpp

Experiments with C++ and comparisons with C
git clone https://git.tronto.net/taming-cpp
Download | Log | Files | Refs | README

sort.cpp (587B)


      1 #include <algorithm>
      2 #include <cstdlib>
      3 #include <ctime>
      4 #include <iostream>
      5 
      6 int a[ARRAYSIZE];
      7 
      8 int main(void) {
      9 	srand(time(NULL));
     10 	for (auto &x : a) x = rand() % 1000000000;
     11 
     12 	struct timespec begin;
     13 	clock_gettime(CLOCK_MONOTONIC, &begin);
     14 
     15 	std::sort(a, a+ARRAYSIZE,
     16 		[](const int &x, const int &y) { return x < y; });
     17 
     18 	struct timespec end;
     19 	clock_gettime(CLOCK_MONOTONIC, &end);
     20 	double time = end.tv_sec - begin.tv_sec +
     21 		(end.tv_nsec - begin.tv_nsec) / 1000000000.0;
     22 
     23 	std::cout << "(" << a[ARRAYSIZE/2] << ") C++ time for " << ARRAYSIZE
     24 		<< " numbers: " << time << "s\n";
     25 }