| 54 | // ---------------------------------------------------------------------------------------- |
| 55 | |
| 56 | void example_using_regular_non_parallel_loops() |
| 57 | { |
| 58 | cout << "\nExample using regular non-parallel for loops\n" << endl; |
| 59 | |
| 60 | std::vector<int> vect; |
| 61 | |
| 62 | // put 10 elements into vect which are all equal to -1 |
| 63 | vect.assign(10, -1); |
| 64 | |
| 65 | // Now set each element equal to its index value. We put a sleep call in here so that |
| 66 | // when we run the same thing with a parallel for loop later on you will be able to |
| 67 | // observe the speedup. |
| 68 | for (unsigned long i = 0; i < vect.size(); ++i) |
| 69 | { |
| 70 | vect[i] = i; |
| 71 | dlib::sleep(1000); // sleep for 1 second |
| 72 | } |
| 73 | print(vect); |
| 74 | |
| 75 | |
| 76 | |
| 77 | // Assign only part of the elements in vect. |
| 78 | vect.assign(10, -1); |
| 79 | for (unsigned long i = 1; i < 5; ++i) |
| 80 | { |
| 81 | vect[i] = i; |
| 82 | dlib::sleep(1000); |
| 83 | } |
| 84 | print(vect); |
| 85 | |
| 86 | |
| 87 | |
| 88 | // Sum all element sin vect. |
| 89 | int sum = 0; |
| 90 | vect.assign(10, 2); |
| 91 | for (unsigned long i = 0; i < vect.size(); ++i) |
| 92 | { |
| 93 | dlib::sleep(1000); |
| 94 | sum += vect[i]; |
| 95 | } |
| 96 | |
| 97 | cout << "sum: "<< sum << endl; |
| 98 | } |
| 99 | |
| 100 | // ---------------------------------------------------------------------------------------- |
| 101 | |