473,396 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

time of execution

I've written this C prog. (insertion sort) which sorts and finds the time of execution.

#include<stdio.h>
#include<conio.h>
#include<time.h>

main()
{
int a[25] , i , j , k;
clock_t start,end;
clrscr();
printf("\n enter the numbers ");
for( i= 0 ; i <= 24 ; i++)

scanf("%d" , &a[i]) ;
start=clock();
for(i =0 ; i<= 24 ; i++ )
{
for( j= 1 ; j<= 24 ; j++ )
{
if(a[i]> a[j])
{
k = a[i];
a[i]=a[j];
a[j] = k ;
}
}
}

printf("\n sorted numbers are ");
for(i = 0 ; i<= 24 ; i++ )
{
printf("%d" , a[i]) ;
}
end=clock();
printf("\n\nTime of execution : %f",(end-start)/CLK_TCK);
getch();
}
---------------
my problem is that it always outputs time of execution = 0.000000 whatever i do.
why it is not showing different values ? what changes should i make to this prog?is there any other way to find time of execution ?
Mar 24 '10 #1
4 2161
jkmyoung
2,057 Expert 2GB
Not sure. Try casting to a double before dividing:
((double)(end-start))/ CLOCKS_PER_SEC);
Mar 24 '10 #2
donbock
2,426 Expert 2GB
From the C Standard

The clock function returns the implementation's best approximation to the processor time used by the program since the beginning of an implementation-defined era related only to the program invocation. To determine the time in seconds, the value returned by the clock function should be divided by the value of the macro CLOCKS_PER_SEC. If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t)-1.

Notice that last sentence. The C Standard does not guarantee that this function accomplishes anything. Check if clock is returning -1. If so, then you need to look for another function, probably not a standard function, to measure elapsed time with.
Mar 24 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
Your program is too fast. Try executing the program, say 100,000 times, in a loop. Do your clock before and after the loop and divide the difference in times by 100,000.
Mar 24 '10 #4
RedSon
5,000 Expert 4TB
You are only sorting 24 numbers? Even something like bubble sort would sort that almost instantly. Worst case performance of that is O(n^2) and if n = 24 thats nothing for a desktop machine. Try sorting a million items or something like that then you will see how long it takes.
Mar 24 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

77
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the...
1
by: Francesc Guim Bernat | last post by:
Dear colleagues, i'm getting in troubles using one XML library with Visual Studio .NET and Xerces with Xalan. When i execute the code i get the next run time error: "Run-Time Check Failure #2...
6
by: cournape | last post by:
Hi there, I have some scientific application written in python. There is a good deal of list processing, but also some "simple" computation such as basic linear algebra involved. I would like to...
5
by: Johannes Lebek | last post by:
Hi there, lately, I experienced a strange thing on my DB2 V8.1 on Windows: Some queries took a very long time. A snapshot discovered the following: Number of executions = 47...
0
by: Ina Schmitz | last post by:
Hi all, I would like to get the total execution time and the cpu time of a query execution. For that purpose, I think SNAP_GET_DYN_SQL is the correct table function. But SNAPSHOT_DYN_SQL...
7
by: Tim Quon | last post by:
Hi Is there any function to get the current time so I can calculate the execution time of my code? What all is in the time.h and sys/times.h? Thanks Tim
38
by: vashwath | last post by:
Might be off topic but I don't know where to post this question.Hope some body clears my doubt. The coding standard of the project which I am working on say's not to use malloc.When I asked my...
5
by: Jeremy | last post by:
I have a core VB service that monitors a database, and based on data in the records will execute code to send email notifications. Problem: I don't want my main program code to halt and wait for...
3
by: iam980 | last post by:
Hello All. We have tested following SQL script from query analyzer: -- Script begin DECLARE @I int; SET @I = 1; WHILE @I < 10000000 BEGIN SET @I = @I + 1; END -- Script end
6
by: Mikhail Kovalev | last post by:
I'm using set_time_limit() to set maximum execution time. Is there a way to check how much time is left at any time during the execution itself?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.