Compare Two Different Program

Dear All

I have a task, to compare time for search a data in 2 different method data search (sequential and binary search), there are program for compare two different program ?
If you have two different programs you can run them with system and measure each running time with clock().

http://www.cplusplus.com/reference/cstdlib/system/
http://www.cplusplus.com/reference/ctime/clock/
thanks for the answers
> compare time for search a data in 2 different method data search (sequential and binary search),
> there are program for compare two different program ?

Simpler (and quite a lot more accurate) to compare the time taken for two different functions (sequential and binary search) within the same program.

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
31
32
33
34
35
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cassert>

bool seq_search( const std::vector<int>& seq, int value )
{ return std::find( std::begin(seq), std::end(seq), value ) != std::end(seq) ; }

bool bin_search( const std::vector<int>& seq, int value )
{ return std::binary_search( std::begin(seq), std::end(seq), value ) ; }

int main()
{
    const int N = 100'000 ;
    std::vector<int> seq ;
    for( int i = 0 ; i < N ; ++i ) seq.push_back(i*2) ;
    int result = 0 ;

    {
        const auto start = std::clock() ;
        for( int i = 0 ; i < N ; ++i ) result += seq_search( seq, i ) ;
        const auto end = std::clock() ;
        std::cout << "seq_search: " << (end-start) * 1000.0 / CLOCKS_PER_SEC << " millisecs\n" ;
    }

    {
        const auto start = std::clock() ;
        for( int i = 0 ; i < N ; ++i ) result -= bin_search( seq, i ) ;
        const auto end = std::clock() ;
        std::cout << "bin_search: " << (end-start) * 1000.0 / CLOCKS_PER_SEC << " millisecs\n" ;
    }

    assert( result == 0 ) ;
} 

http://coliru.stacked-crooked.com/a/5f3c683e5a0b15b8
1
2
3
4
int start_s=clock();
	// the code you wish to time goes here
int stop_s=clock();
cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;


I've Already use that to my code, but why the ouput always 0s ??
> but why the ouput always 0s ??

The code that is being timed executes in a very short time; it finishes before the clock has ticked even once.

The result of std::clock() need not necessarily be int;
either use type-deduction with auto or specify the type as std::clock_t.
Topic archived. No new replies allowed.