Compare execution time in C language

In summary, the conversation discusses the attempt to compare execution times for DDA and Bresenham's Line algorithm using a C program. The speaker used the clock_t command but encountered issues with the granularity of the measurements, leading to inconsistent results. They are seeking advice on how to improve their measurement method.
  • #1
Vagrant
195
1

Homework Statement


I want to compare the execution times for DDA and Bresenham's Line algorithm using a C program. I used the clock_t command; I'm getting the execution time for Bresenham’s algorithm as 0.164835 but 0 for DDA.

3. The Attempt at a Solution

clock_t start,end;
start=clock();
Bresenham's line algorithm code
end=clock();
printf("time=%f\n",(end-start)/CLK_TCK);

start=clock();
DDA line algorithm code
end=clock();
printf("time=%f\n",(end-start)/CLK_TCK);

Also I'm getting t=0.164835 for both codes separately. Whereas DDA line algorithm should take more time. What is it that I’m doing wrong?
 
Physics news on Phys.org
  • #2
Your times aren't granular enough for what you're trying to measure. The basic unit of the clock_t type is seconds, which is way too large for measuring operations that are happening at clock rates in the gigahertz range.
 

1. Why is it important to compare execution time in C language?

Comparing execution time in C language allows for optimization of code and can improve the speed and efficiency of a program. It also helps to identify any potential bottlenecks or areas of improvement in the code.

2. How do you measure execution time in C language?

The clock() function in the time.h library can be used to measure execution time in C language. It returns the number of clock ticks elapsed since the start of the program, which can then be converted to seconds or other units.

3. What factors can affect execution time in C language?

Execution time in C language can be affected by various factors such as the complexity of the algorithm, the size of input data, the hardware and operating system of the computer, and the compiler used to compile the code.

4. How can you improve execution time in C language?

To improve execution time in C language, you can optimize the algorithm, use efficient data structures, minimize the number of operations performed, and consider using parallel programming techniques. Additionally, upgrading hardware or choosing a more efficient compiler can also help improve execution time.

5. Can execution time be accurately compared between different programs or computers?

No, execution time cannot be accurately compared between different programs or computers as there are too many variables that can affect it. It is best to compare execution time within the same program and on the same computer to get a more accurate measurement.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
997
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top