perf_stl_find.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2015 Jakub Szuppe <j.szuppe@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. // Max integer that can be generated by rand_int() function.
  15. int rand_int_max = 25;
  16. int rand_int()
  17. {
  18. return static_cast<int>((rand() / double(RAND_MAX)) * rand_int_max);
  19. }
  20. int main(int argc, char *argv[])
  21. {
  22. perf_parse_args(argc, argv);
  23. std::cout << "size: " << PERF_N << std::endl;
  24. // create vector of random numbers on the host
  25. std::vector<int> host_vector(PERF_N);
  26. std::generate(host_vector.begin(), host_vector.end(), rand_int);
  27. // trying to find element that isn't in vector (worst-case scenario)
  28. int wanted = rand_int_max + 1;
  29. // result
  30. std::vector<int>::iterator host_result_it;
  31. perf_timer t;
  32. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  33. t.start();
  34. host_result_it = std::find(host_vector.begin(), host_vector.end(), wanted);
  35. t.stop();
  36. }
  37. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  38. // verify
  39. if(host_result_it != host_vector.end()){
  40. std::cout << "ERROR: "
  41. << "host_result_iterator != "
  42. << "host_vector.end()"
  43. << std::endl;
  44. return -1;
  45. }
  46. return 0;
  47. }