Why Does One Piece of Code Run Slower Than Another?

  • Thread starter STEMucator
  • Start date
  • Tags
    Code
In summary, the conversation discusses two pieces of code that have similar outputs but different run times. The code with an implicit conditional runs faster for smaller data sets, but for larger data sets, the code without the conditional is faster. This may be due to the compiler or processor, and further investigation is recommended.
  • #1
STEMucator
Homework Helper
2,076
140

Homework Statement



Not homework, but I'm wondering why a certain piece of code seems to run faster than another.

Homework Equations

The Attempt at a Solution



I'll mention a few relevant structs to be more clear:

Code:
struct intlist {
  int  *elems;
  int  capacity;  /* Maximum number of elements in the list. */
  int  size;       /* Current number of elements in the list */
};

typedef struct intlist IntList;

Here's a piece of code that prints out the heap allocated contents of elems:

Code:
/* Print the list pointed to by parameter list to the console.
* Terminate the program via assert if list is NULL.
*/

void intlist_print(const IntList *list){
  assert(list != NULL);
 
  if(list->size == 0)
  printf("[]");
  else{
  clock_t tic = clock();
 
  printf("[");
  for(int i=0;i<list->size;i++)
  i != list->size-1 ? printf("%d ", list->elems[i]) :  printf("%d]", list->elems[i]);
 
  clock_t toc = clock();
  printf("\nElapsed time: %f seconds.\n", (double)(toc - tic) / CLOCKS_PER_SEC);
  }
}

This code produces these results:

Code:
Expected output: [1 5 -3 9 1 5 -3 9 1 5]
Actual output:  [1 5 -3 9 1 5 -3 9 1 5]
Elapsed time: 0.000002 seconds.

Now here's another piece of code, which does the same thing, but I originally thought would run faster:

Code:
void intlist_print(const IntList *list){
  assert(list != NULL);
 
  if(list->size == 0)
  printf("[]");
  else{
  clock_t tic = clock();
 
  printf("[");
  for(int i = 0; i < list->size-1; i++)
      printf("%d ", list->elems[i]);
  printf("%d]", list->elems[list->size-1]);
 
  clock_t toc = clock();
  printf("\nElapsed time: %f seconds.\n", (double)(toc - tic) / CLOCKS_PER_SEC);
  }
}

The results are below:

Code:
Expected output: [1 5 -3 9 1 5 -3 9 1 5]
Actual output:  [1 5 -3 9 1 5 -3 9 1 5]
Elapsed time: 0.000003 seconds.

So for large enough ##n##, it would appear that the code without the implicit conditional runs slower for some reason. Why is this? I would think checking an extra if statement every loop would cost more time than simply iterating through the list and printing the contents. Is this related to something compiler or cpu specific?
 
Physics news on Phys.org
  • #2
I am dubious about your conclusion. The time is only measured to one sig dig. What if it were measured to, say, 4 sig digs?

What if the actual values are
.000002999 versus .000003000?

Would you still wonder if one is meaningfully faster than the other?

Easy way to check. Run those sims on a list 1000 times the size.
 
Last edited:
  • #3
DaveC426913 said:
I am dubious about your conclusion. The time is only measured to one sig dig. What if it were measured to, say, 4 sig digs?

What if the actual values are
.000002999 versus .000003000?

Would you still wonder if one is meaningfully faster than the other?

Easy way to check. Run those sims on a list 1000 times the size.

I ran some more tests for ##n = 20 000##. The code without the conditional ran in 0.005440 seconds. The code with the conditional ran in 0.005241 seconds. The code with the conditional was faster for some reason.

To get a better sense I ran it with ##n = 1 000 000##. The code without the conditional ran in 0.219394 seconds. The code with the conditional ran in 0.219831 seconds. Now the code without the conditional ran faster.

There appears to be some ##n## such that the efficiency of the conditional code falls below that of the more brute force approach. What's interesting is that for small enough ##n##, the code with the conditional runs faster for some reason.
 
Last edited:
  • Like
Likes DaveC426913
  • #4
So, would you consider a 1-in-500 spread on a million record dataset to be of concern? Even if you scaled that up without limit, it's still 1-in-500 (or so). If one took 2 days to finish, would it matter if the other took 2 days and the other took 2 days plus 5 minutes...?

Or is this more of an academic interest?

The magic that happens with things like memory allocation and discrete operations in a processor and a language are pretty arcane. But there is probably some information out there about for loops and conditonals for most popular languages. Couldn't tell you though.

If you wanted to pursue it on your own,. I'd suggest paring the code down to its bare minimum so it's much more intuitive to see what's happening and to eliminate extraneous factors.
 
Last edited:
  • #5
DaveC426913 said:
So, would you consider a 1-in-500 spread on a million record dataset to be of concern? Even if you scaled that up without limit, it's still 1-in-500 (or so). If one took 2 days to finish, would it matter if the other took 2 days and the other took 2 days plus 5 minutes...?

Or is this more of an academic interest?

The magic that happens with things like memory allocation and discrete operations in a processor and a language are pretty arcane. But there is probably some information out there about for loops and conditonals for most popular languages. Couldn't tell you though.

If you wanted to pursue it on your own,. I'd suggest paring the code down to its bare minimum so it's much more intuitive to see what's happening and to eliminate extraneous factors.

This is purely out of academic interest. I've been looking into optimizing code for specific applications.

If you knew the size of the data set you could select the according algorithm.

On topic, I've heard that code can tend to behave differently on different machines. I'm looking around, but it all seems really specific.
 

1. Why does one piece of code run slower than another?

There are several reasons why one piece of code may run slower than another. These include the complexity of the code, the efficiency of the algorithms used, and the hardware and software environment in which the code is executed.

2. How can I determine why my code is running slower?

To determine why your code is running slower, you can use profiling tools to analyze the performance of your code. These tools can help identify specific areas of your code that are taking longer to execute and suggest ways to improve its speed.

3. Can inefficient coding practices affect the speed of my code?

Yes, inefficient coding practices such as unnecessary loops or redundant calculations can significantly impact the speed of your code. It is important to use efficient coding techniques to optimize the performance of your code.

4. Are there any ways to improve the speed of slow-running code?

Yes, there are several ways to improve the speed of slow-running code. These include optimizing algorithms, reducing the amount of unnecessary calculations, and using parallel processing techniques to distribute the workload.

5. Can the hardware or software environment affect the speed of my code?

Yes, the hardware and software environment can have a significant impact on the speed of your code. Factors such as the processor speed, memory capacity, and operating system can all affect the performance of your code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
868
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top