So I have completed my program thus far to take an array the user desingnates and breaks with any letter.
After outputting what the array holds it sorts and re prints. I want to also include the time it takes this to happen.

I am trying to use the gettimeofday function. I am getting "not declared" in the header
It's 3 am too, that could be it. Here is my code: Main.cpp here

   int main(int argc, char * argv[])


{


cout << "Welcome To The Sorter! V 1.0..." << endl << endl;
cout << "----------------------------------------------------" << endl;
cout << "|  Please Enter Numbers That You Would Like Sorted |" << endl;
cout << "|  Enter Any Letter To Exit Sequence               |" << endl;
cout << "----------------------------------------------------" << endl;
Sorting derp;
int x = 0;
int Myarray[200];
int copied[200];
char holder[128] = {0};
while(derp.isnumber(holder) == true)
{
    cin.getline(holder,256);
    if(derp.isnumber(holder))
    {
        int c = atoi(holder);
        Myarray[x] = c;
        x = x + 1;
        derp.print_array(Myarray,x);
    }

}
cout << "----------------------------------" << endl;
cout << "| Breaking from numerical input  |" << endl;
cout << "----------------------------------" << endl;
derp.copy_array(Myarray,copied,x);
derp.selection_sort(copied,x);
derp.print_array(copied,x);
derp.copy_array(Myarray,copied,x);
struct timeval *start, *finish;
gettimeofday(&start, NULL);
derp.selection_sort(Myarray,x);
derp.diff_in_micros(finish, start);
gettimeofday(&finish, NULL);
cout << "Time (us) : " << diff_in_micros(finish,start) << endl;
//derp.linear_insertion_sort;
//derp.print_array(copied,x);

return 0;

}

Header.cpp // Leaving out the sorting and unimportant functions

#include "Sorting.h"
#include <iostream>

using namespace std;


long Sorting::diff_in_micros(timeval finish, timeval start)
{
    long sec = (finish.tv_sec - start.tv_sec) * 1000;
    long us = (finish.tv_usec - start.tv_usec);
    return sec + us;
}

Header File:

#ifndef SORTING_H
#define SORTING_H


class Sorting
{
    public:
        Sorting();
        void copy_array(const int src[], int dst[], int size);
        void print_array(const int array[], int size);
        void selection_sort(int array[], int size);
        void linear_insertion_sort(int array[], int size);
        void binary_insertion_sort(int array[], int size);
        bool isnumber(char *str);
        long diff_in_micros(timeval finish,timeval start);
        virtual ~Sorting();
    protected:

    private:
};

#endif // SORTING_H

Recommended Answers

All 4 Replies

Two things:

  • gettimeofday takes a pointer to a struct timeval, you are providing a pointer to a pointer
  • I do not see anywhere where you include <sys/time.h>

Ah i see...
The header is there I forgot to paste those in with the code.

Ok Even more simply here, I have implented the function in the main just to get by the defining problem. However I get a time of 0?

Am I doing something wrong here?

#include <iostream>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include "Sorting.h"
#include <string.h>
#include <iostream>

using namespace std;

long diff_in_micros(timeval finish, timeval start)
{
    long sec = (finish.tv_sec - start.tv_sec) * 1000;
    long us = (finish.tv_usec - start.tv_usec);
    return sec + us;
    cout << sec + us;
}



int main(int argc, char * argv[])
{


cout << "Welcome To The Sorter! V 1.0..." << endl << endl;
cout << "----------------------------------------------------" << endl;
cout << "|  Please Enter Numbers That You Would Like Sorted |" << endl;
cout << "|  Enter Any Letter To Exit Sequence               |" << endl;
cout << "----------------------------------------------------" << endl;
timeval start;
timeval finish;
Sorting derp;
int x = 0;
int Myarray[200];
int copied[200];
char holder[128] = {0};
while(derp.isnumber(holder) == true)
{
    cin.getline(holder,256);
    if(derp.isnumber(holder))
    {
        int c = atoi(holder);
        Myarray[x] = c;
        x = x + 1;
        derp.print_array(Myarray,x);
    }

}
cout << "----------------------------------" << endl;
cout << "| Breaking from numerical input  |" << endl;
cout << "----------------------------------" << endl;
derp.copy_array(Myarray,copied,x);
\
gettimeofday(&start, NULL);
derp.selection_sort(copied,x);
gettimeofday(&finish, NULL);
cout << "Time (us): " << diff_in_micros(finish, start) << "\n";
derp.print_array(copied,x);
derp.copy_array(Myarray,copied,x);
derp.selection_sort(Myarray,x);
//derp.linear_insertion_sort;
//derp.print_array(copied,x);

return 0;

}

The following works for me:

#include <iostream>
#include <sys/time.h>
#include <unistd.h>

int main () {
    struct timeval start, end;
    gettimeofday (&start, NULL);
    sleep (5);
    gettimeofday (&end, NULL);
    double s = (double)start.tv_sec + start.tv_usec / 1000000.0;
    double e = (double)end.tv_sec + end.tv_usec / 1000000.0;
    std::cout << e - s << std::endl;
    return 0;
}

My output is (in one example): 5.00169

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.