How to add time in Milliseconds?

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;
}

}
return 0;
}
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";
}
My compiler doesn't support that code gunnerfunner
time to get a new compiler then ;)
compiler with IDE: https://sourceforge.net/projects/codeblocks/
compiler only: https://nuwen.net/mingw.html
Last edited on
Use std::clock() in <ctime> http://en.cppreference.com/w/cpp/chrono/c/clock

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() ;

    const double elapsed_processor_time_millisecs = (end-start) * 1000.0 / CLOCKS_PER_SEC ;
    // print it out if required
}

std::chrono::steady_clock is the wrong clock for measuring the (single-threaded) performance of an algorithm

could you please clarify? the source of my comments was 'The C++ Standard Library (2nd ed), Josuttis' where he mentions
the steady_clock is important to compare or compute the difference of two times in your program, ...
and mentions the reasons, etc - chapter 5.7.3. thanks
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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <ctime>
#include <chrono>
#include <thread>

int main()
{
    using namespace std::chrono ;
    
    const auto startp = std::clock() ;
    const auto startw = steady_clock::now() ;
    
    volatile double 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 ;

    const auto endw = steady_clock::now() ;
    const auto 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" ;
}

clang++ -std=c++14 -stdlib=libc++ -Wall -Wextra -pedantic-errors main.cpp -lsupc++ && time ./a.out
elapsed steady clock time: 300 msecs
   elapsed processor time: 100.406 msecs

real	0m0.305s
user	0m0.100s
sys	0m0.008s

http://coliru.stacked-crooked.com/a/321a6e5cf932f611
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
Last edited on
Topic archived. No new replies allowed.