perf_stl_count.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #include <algorithm>
  11. #include <iostream>
  12. #include <vector>
  13. #include "perf.hpp"
  14. int rand_int()
  15. {
  16. return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
  17. }
  18. int main(int argc, char *argv[])
  19. {
  20. perf_parse_args(argc, argv);
  21. std::cout << "size: " << PERF_N << std::endl;
  22. // create vector of random numbers on the host
  23. std::vector<int> host_vector(PERF_N);
  24. std::generate(host_vector.begin(), host_vector.end(), rand_int);
  25. // count values equal to four in the vector
  26. size_t count = 0;
  27. perf_timer t;
  28. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  29. t.start();
  30. count = std::count(
  31. host_vector.begin(), host_vector.end(), 4
  32. );
  33. t.stop();
  34. }
  35. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  36. std::cout << "count: " << count << std::endl;
  37. return 0;
  38. }