Why Does One Piece of Code Run Slower Than Another?

  • Thread starter Thread starter STEMucator
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Discussion Overview

The discussion revolves around the performance differences between two pieces of code that print the contents of a list. Participants explore why one version appears to run faster than the other, considering factors such as conditional statements and the impact of varying input sizes.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents two versions of a function that prints a list, noting that the version with an implicit conditional seems to run slower than the one without it.
  • Another participant questions the reliability of the timing measurements, suggesting that measuring to more significant digits could change the interpretation of the results.
  • A later reply shares additional timing results for larger input sizes, indicating that the performance relationship between the two versions may depend on the size of the input list.
  • Some participants discuss the relevance of performance differences in practical scenarios, questioning whether small discrepancies matter in the context of larger datasets.
  • There is mention of the complexity of how memory allocation and processor operations can affect performance, suggesting that this topic may require deeper investigation.

Areas of Agreement / Disagreement

Participants express differing views on the significance of the performance differences observed, with some questioning the validity of the conclusions drawn from the timing results. The discussion remains unresolved regarding the implications of these performance variations.

Contextual Notes

Participants note that performance may vary based on machine specifications and that the results could depend on specific conditions, such as the size of the dataset and the environment in which the code is executed.

Who May Find This Useful

This discussion may be of interest to programmers and computer scientists focused on code optimization, performance analysis, and those curious about the effects of algorithmic choices on execution time.

STEMucator
Homework Helper
Messages
2,076
Reaction score
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
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:
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   Reactions: DaveC426913
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:
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.
 

Similar threads

  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
4K