fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <ctime>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7. bool linearSearch(std::vector<int>&primes, int number, int range) {
  8. for (int i = 0; i < range; i++) {
  9. if (primes[i] == number)
  10. return true;
  11. }
  12. return false;
  13. }
  14.  
  15.  
  16. void timeLinearSearch(std::vector<int>& primes) {
  17. clock_t start, stop;
  18. size_t NRND = 15000; // 15000 primes per clock
  19.  
  20. for (int N = 50000; N <= 100000; N += 50000) // increase by 50k each iteration
  21. {
  22. for (int repeat = 0; repeat < 5; repeat++) {
  23. start = clock();
  24. int count = 0;
  25. for (int j = 0; j < NRND; j++) {
  26. count += linearSearch(primes, rand(), N);
  27. }
  28. stop = clock();
  29. std::cout << stop - start << ", " << N << " " << count << std::endl;
  30. }
  31. }
  32. }
  33.  
  34. int main() {
  35. vector<int> v;
  36. for (int i = 0 ; i != 600000 ; i++) {
  37. v.push_back(rand());
  38. }
  39. timeLinearSearch(v);
  40. return 0;
  41. }
Success #stdin #stdout 3.29s 3472KB
stdin
Standard input is empty
stdout
215529, 50000 0
215231, 50000 2
221221, 50000 0
217863, 50000 0
216557, 50000 0
445586, 100000 3
439603, 100000 1
434343, 100000 1
434926, 100000 3
437629, 100000 0