Algorithmic Analysis, Help With Timer Class

Hi I am having trouble getting my timer class to properly output the correct duration of time. I have tried a lot of things and have not been able to get the proper outcome, If someone could take a look at my code and maybe just point out or assist with fixing, would be much appreciated.

This is meant to be a simple demo program to show that a linear search is inefficient compared to a binary search, except my timers are not printing correct duration.

Codes may use of call to system (developing on a windows machine currently)
Thanks in advance, and i apologize for posting long code.

timer.h
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef TIMER_H
#define TIMER_H

#include <iostream>
#include <time.h>
#include <ctime>
#include "timer_exception.h"


class timer {
    friend std::ostream &operator <<(std::ostream &os, timer &timer){
           os << timer.hours()<<":"<<timer.minutes()<<":"<<timer.seconds()<<":"<<timer.ticks();
           return os;
           }
	
	public:
           
		timer();
		void           start();
		void           stop();
		void           restart();
		bool           isRunning();
		clock_t getTime();
		int            ticks(){
                                 if(!running){
                                     return   (getTime() % CLOCKS_PER_SEC);
                                     }
                               else throw TimerException("Cannot get time while running");
                               }
		int            seconds(){
                                 if(!running){
                                 return (getTime()/CLOCKS_PER_SEC);
                                 }else throw TimerException("Cannot get time while running");
                                 
                       }
		int            minutes(){
                       if(!running){
                                    return (((getTime()/CLOCKS_PER_SEC)/60) %60);
                                    }else throw TimerException("Cannot get time while running");
                                    
                                    }
		int            hours(){
                       if(!running){
                                    return (((getTime() / CLOCKS_PER_SEC) / 60) / 60);
                                    }
                       else throw TimerException("Cannot get time while running");
                       }

	private:
		bool           resetted;
	    bool           running;
		unsigned long  beg;
		unsigned long  end;
		unsigned long duration;
		
	
		
};

timer::timer() {
	resetted = true;
	running = false;
	beg = 0;
	end = 0;
}

void timer::start(){
     if(! running) {
		if(resetted)
			beg = (unsigned long) clock();
		else
			beg -= end - (unsigned long) clock();
		running = true;
		resetted = false;
	}
	else throw TimerException("Timer is already running");
}

void timer::stop(){
     if(running){
                 end = (unsigned long) clock();
                 running = false;
                 }
     else throw TimerException("Timer is not running");
}

void timer::restart(){
     if(running){
                 stop();
                 resetted=true;
                 beg=0;
                 end=0;
                 start();
                 }
}

bool timer::isRunning(){
     return running;
     }

clock_t timer::getTime(){
        duration=(end-beg);
        clock_t t(duration);
		return t;
}            

#endif     



timer_exception.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef TIMER_EXCEPTION_H
#define TIMER_EXCEPTION_H

#include <iostream>
#include <string>
#include "timer.h"

class TimerException {
	friend std::ostream &operator <<(std::ostream &os, TimerException &e) {
		os << "*** TimerException *** " << e.message;
		return os;
	}
public:
	TimerException(std::string message) : message(message) {}
private:
	 std::string message;
};

#endif



timer_demo2.cpp
Used to show difference in 2 searching methods (linear and binary)

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <fstream>
#include "timer.h"
#include "timer_exception.h"

using namespace std;

bool binarySearch(int *arr, int lo, int hi, int val) {
	if (lo > hi) return false;
	int mid = (lo + hi) / 2;
	if (arr[mid] == val) 
		return true;
	else if (arr[mid] & val) 
		return binarySearch(arr, mid+1+1, hi, val);
	else 
		return binarySearch(arr, lo, mid-1, val);
}   

void bubbleSort(int arr[], int n) {
	bool isSorted = false;
	for (int last = n-1; last > 0 && !isSorted; last--) {
		isSorted = true;
		for (int i = 0; i < last; i++)
			if (arr[i] > arr[i+1]) {
				swap(arr[i], arr[i+1]);
				isSorted = false;
			}
		if (last % 1000 == 0) cerr << ".";
	}
}         
    

int main(){
    timer t;
    int arr[100000];
    int searchFor[1000];
    int numbersfound[1000];
    
    ifstream ifile, ifile2;
    
    ifile.open("1000000_random_numbers.txt");
    if(ifile.fail()){
                     cout<<"Failed to open file"<<endl;
                     return 0;
                     }
    ifile.seekg(27);
                          for(int i=0;i<100000;i++){
                                  ifile >>arr[i];
                                  }
                                  
    ifile.close();
/*    for(int i=0;i<10000;i++){
        cout<<arr[i]<<" ";
        }
*/        

    ifile2.open("numbers_to_search_for.txt");
    if(ifile2.fail()){
                      cout<<"Failed to open file"<<endl;
                      return 0;
                      }
    ifile2.seekg(22);
    
                     for(int i=0;i<1000;i++){
                           ifile2 >> searchFor[i];
                           }
  /*  for(int i=0;i<1000;i++){
        cout<<searchFor[i]<<" ";
        }
    */                       
    ifile2.close();                       

// Time a Linear Search
 t.start();  
             
    
   for (int i=0; i<100000; i++) {
        for(int j=0;j<1000;j++){
                if(searchFor[j]==arr[i]){
                                         numbersfound[i]=searchFor[j];
                                         }
                                         }
                                         }
t.stop();

//Sort
bubbleSort(arr,100000);

                            
timer t2;

//Time a Binary Search
int numbersfound2[1000];

t2.start();
for(int i=0;i<1000;i++){

if(binarySearch(arr, arr[0],arr[99999],searchFor[i])){
                     numbersfound2[i]=searchFor[i];
                     }
}
t2.stop();

    
    
    
cout << endl<< "The duration of the linear search  is  " << t << endl;
cout << "The duration of the binary search is " << t2 << endl;

system ("pause");
return 0;
}
Last edited on
bump, going crazy any assistance would be greatly appreciated!

Topic archived. No new replies allowed.