I'm making a linear search program and I can't seem to get the right time elapsed. This is my linear search algorithm without adding the time. I need help on adding the time, I don't know where to put it and what code to type.
while (search !=-1)
{
int inc=0;
while (array[inc] != search && inc < N)
{
inc++;
}
if(array[inc] == search)
{
cout << "The key " << search;
cout << " is found on the index " << inc;
cout << "." << endl;
break;
}
else
{
cout<< "\nSorry, I can't seem to find it." << endl;
break;
}
The C++ standard library provides three clocks: (a) system_clock (b) steady_clock and (c) high_resolution_clock
Of these only steady_clock gives the guarantee that it never gets adjusted for e.g when other clocks go back/forward in fall/spring. So use steady_clock to compare or compute the difference b/w two times in your program:
1 2 3 4 5 6 7 8 9 10 11 12
#include<iostream>
#include <chrono>
int main() {
auto begin = std::chrono::steady_clock::now();
int x;
std::cin >> x; // wait for user input
auto end = std::chrono::steady_clock::now();
auto dur = end - begin;
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
std::cout << ms << "\n";
}
std::chrono::steady_clock is the wrong clock for measuring the (single-threaded) performance of an algorithm.
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <ctime>
int main()
{
const std::clock_t start = std::clock() ;
// code to perform linear search (or call the function if it is a function)
const std::clock_t end = std::clock() ;
constdouble elapsed_processor_time_millisecs = (end-start) * 1000.0 / CLOCKS_PER_SEC ;
// print it out if required
}
Intervals from std::chrono::steady_clock give elapsed real time;
intervals from std::clock() give the processor time used for execution.
std::clock time may advance faster or slower than the wall clock, depending on the execution resources given to the program by the operating system. For example, if the CPU is shared by other processes, std::clock time may advance slower than wall clock. http://en.cppreference.com/w/cpp/chrono/c/clock
#include <iostream>
#include <ctime>
#include <chrono>
#include <thread>
int main()
{
usingnamespace std::chrono ;
constauto startp = std::clock() ;
constauto startw = steady_clock::now() ;
volatiledouble d = 0 ;
// measure the time taken to increment d by 23.8 20'000'000 times
for( int i = 0 ; i < 10'000'000 ; ++i ) d += 23.8 ;
// assume that there is heavy contention for the processor and mid-way through our loop,
// the operating system gives the resource to other competing processes for 200 msecs
// we simulate the above situation here with a sleep
std::this_thread::sleep_for( milliseconds(200) ) ;
for( int i = 10'000'000 ; i < 20'000'000 ; ++i ) d += 23.8 ;
constauto endw = steady_clock::now() ;
constauto endp = std::clock() ;
std::cout << "elapsed steady clock time: " << duration_cast<milliseconds>( endw - startw ).count() << " msecs\n"
<< " elapsed processor time: " << double( endp - startp ) * 1000 / CLOCKS_PER_SEC << " msecs\n" ;
}
thank you JLBorges, very clearly illustrated (yet again!)
edit: i must add though i'm still trying to square the author's comments with the example above. perhaps i missed something