Menu

[Solved]Lab 10a Measure Program Execution Time One Easy Way Measure Program Execution Time Encapsu Q37065945

Lab 10A Measure the program execution time

One easy way to measure the program execution time isencapsulate the useful timing functionality of C++ chrono library,This is illustratedinhttps://www.learncpp.com/cpp-tutorial/8-16-timing-your-code/

The example usingChronoTimer.cpp (Github/m10) is anexample program using this chrono Timer class object to measure theSorting on on an integer array of 10000 random integers based onthe Bubble Sort vs. the C++ built-in Sort (anhttps://www.quora.com/Which-sorting-algorithm-does-STL-standard-template-library-use-in-c++.)

Please measure the performance of sorting the same array used inthe usingChronoTimer.cpp example between the InsertionSort vs. the Selection Sort.

Starter

usingChronoTimer.cpp (Github/m10):

#include <chrono> // chrono#include <cstdlib> // srand, rand#include <ctime> // time#include <algorithm> // sort#include <iostream>class Timer {// Type aliases to make accessing nested type easierusing clock_t = std::chrono::high_resolution_clock;using second_t = std::chrono::duration<double,std::ratio<1> >;std::chrono::time_point<clock_t> m_beg;public:Timer() : m_beg(clock_t::now()) {}void reset() { m_beg = clock_t::now(); }double elapsed() const {returnstd::chrono::duration_cast<second_t>(clock_t::now() -m_beg).count();}};void bubbleSort(int* a, int size) {for(int i=0; i<size; i++)for(int j=0; j<size-1; j++) {if(a[j] > a[j+1]) {int t = a[j]; a[j] = a[j+1]; a[j+1] = t;}}}void show(int *a) {for(int i=0; i<5; i++)std::cout << a[i] << ” “;std::cout << std::endl;}int main() {const int Size = 10000;int array1[Size];int array2[Size];srand (time(NULL));int fill;for(int i=0; i<Size; i++) {array1[i] =array2[i] = rand() % 100003; // next prime 10X}show(array1);show(array2);Timer t1;bubbleSort(array1, Size);double run1 = t1.elapsed();show(array1);Timer t2;std::sort(array2, array2+Size);double run2 = t2.elapsed();show(array2);std::cout << “Bubble Sort Time: ” << run1 <<” secondsn”<< “CPP Sort Time: ” << run2 << “secondsn”<< “Efficientcy: ” << int( run1/run2 );return 0;}

Submit

  • myInsertionVsSelectionSortTime.cpp
  • test run result

Expert Answer


Answer to Lab 10A Measure the program execution time One easy way to measure the program execution time is encapsulate the useful … . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *