1

Im trying to write a program for calculating time of execution for different algorithms and for that I need to give different sizes to an array for each algorithm.


I tried to use a loop for doing that but it's not working as I wished. Here is my code :

int main()
{
    clock_t TiCal;
    
    int size;
    for(int p = 5; p < 15; p++)         // This loop is to provide different sizes for our test
    {
        int arr[size];
        cout<<"Ordered array is : { ";
        for(int j = 0; j<size; j++)         // We just wanted to test binary search so I skipped the ordering part
        {
        arr[j] = j;
        cout<<arr[j]<<" ";
        }
        cout<<"}"<<endl;
    
        int key = 9;                            // Program will search for key in array
        int n = sizeof(arr) / sizeof(arr[0]);   // n is size of array
    
        TiCal = clock();                        // TiCal is our variable to save time of execution
        int result = BinSh(arr, 0, n - 1, key);
        Sleep(1);
        TiCal = clock() - TiCal;
        cout<<"Consumed time for size "<<p<<" of binary search is : "<< (float)TiCal/CLOCKS_PER_SEC << " seconds"<<endl;
    
        (result == -1) ? cout << "Element is not present in array"<<endl
             
                       : cout << "Element is present at index " << result<<endl;
    }
    
    return 0;
}
sun tea
  • 13
  • 2
  • your code doesn't even compile `int arr[size];` – arutar Dec 30 '21 at 20:47
  • 2
    What is the expected output and what are you getting? "not working as i wished" doesn't say much. Why do you call sleep(1)? You should also consider using the chrono library instead of C style clock_t. Take a look here https://stackoverflow.com/questions/2808398/easily-measure-elapsed-time – Jane Doe Dec 30 '21 at 20:49
  • In Standard-compliant C++ the size of the array must be known at compile time. Consider moving the body of the loop into a template function that accepts the array size as a template parameter. – user4581301 Dec 30 '21 at 20:54
  • 1
    Side note: When using chrono, don't get sucked into using the impressively-named `high_resolution_clock`. Sometimes it uses a time source where the time can be changed out from underneath you mid-test and ruin the results. Explicitly use `steady_clock` unless your testing finds that it has insufficient resolution, and if it does, then do things the hard way with system-specific monotonic timers. – user4581301 Dec 30 '21 at 20:57
  • @JaneDoe I expected my out put to show me arrays with their sizes and elements but it shows empty arrays every time. I used sleep(1) so I could measure the time taken to execute that part of code, By deleting sleep(1) it shows 0 seconds in output. – sun tea Dec 30 '21 at 21:08
  • 1
    @suntea How does that even make sense - trying to measure the execution time of some function, by artificially adding 1 second to it by sleeping? You might need higher precision than just seconds. The chrono library makes it very easy to measure time down to nanoseconds. And as others already pointed out, your code is ineherntly wrong. `size` is untialized. I'm suprised it even compiles. – Jane Doe Dec 30 '21 at 21:19

0 Answers0