Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 156833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:23:48+00:00 2026-05-11T10:23:48+00:00

I witnessed the following weird behavior. I have two functions, which do almost the

  • 0

I witnessed the following weird behavior. I have two functions, which do almost the same – they measure the number of cycles it takes to do a certain operation. In one function, inside the loop I increment a variable; in the other nothing happens. The variables are volatile so they won’t be optimized away. These are the functions:

unsigned int _osm_iterations=5000;  double osm_operation_time(){     // volatile is used so that j will not be optimized, and ++ operation     // will be done in each loop     volatile unsigned int j=0;     volatile unsigned int i;     tsc_counter_t start_t, end_t;     start_t = tsc_readCycles_C();     for (i=0; i<_osm_iterations; i++){        ++j;     }     end_t = tsc_readCycles_C();     if (tsc_C2CI(start_t) ==0 || tsc_C2CI(end_t) ==0 || tsc_C2CI(start_t) >= tsc_C2CI(end_t))          return -1;     return (tsc_C2CI(end_t)-tsc_C2CI(start_t))/_osm_iterations; }  double osm_empty_time(){     volatile unsigned int i;     volatile unsigned int j=0;     tsc_counter_t start_t, end_t;     start_t = tsc_readCycles_C();     for (i=0; i<_osm_iterations; i++){         ;     }     end_t = tsc_readCycles_C();     if (tsc_C2CI(start_t) ==0 || tsc_C2CI(end_t) ==0 || tsc_C2CI(start_t) >= tsc_C2CI(end_t))         return -1;     return (tsc_C2CI(end_t)-tsc_C2CI(start_t))/_osm_iterations; } 

There are some non-standard functions there but I’m sure you’ll manage.

The thing is, the first function returns 4, while the second function (which supposedly does less) returns 6, although the second one obviously does less than the first one.

Does that make any sense to anyone?

Actually I made the first function so I could reduce the loop overhead for my measurement of the second. Do you have any idea how to do that (as this method doesn’t really cut it)?

I’m on Ubuntu (64 bit I think).

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:23:49+00:00Added an answer on May 11, 2026 at 10:23 am

    I can see a couple of things here. One is that the code for the two loops looks identical. Secondly, the compiler will probably realise that the variable i and the variable j will always have the same value and optimise one of them away. You should look at the generated assembly and see what is really going on.

    Another theory is that the change to the inner body of the loop has affected the cachability of the code – this could have moved it across cache lines or some other thing.

    Since the code is so trivial, you may find it difficult to get an accuate timing value, even if you are doing 5000 iterations, you may find that the time is inside the margin for error for the timing code you are using. A modern computer can probably run that in far less than a millisecond – perhaps you should increase the number of iterations?

    To see the generated assembly in gcc, specify the -S compiler option:

    Q: How can I peek at the assembly code generated by GCC?

    Q: How can I create a file where I can see the C code and its assembly translation together?

    A: Use the -S (note: capital S) switch to GCC, and it will emit the assembly code to a file with a .s extension. For example, the following command:

    gcc -O2 -S -c foo.c

    will leave the generated assembly code on the file foo.s.

    If you want to see the C code together with the assembly it was converted to, use a command line like this:

    gcc -c -g -Wa,-a,-ad [other GCC options] foo.c > foo.lst

    which will output the combined C/assembly listing to the file foo.lst.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have now witnessed two companies move to agile development with Scrum. In both
I have a project in which we would like to do the following :
Having witnessed in various open source projects, in which I have been involved, several
I have Database mirroring setup between two SQl 2005 servers (no witness). Using Tsql,
I have witnessed how Internet Explorer 8 (IE8) is in Windows XP and how
We have a SQL 2005 database server which uses high safety (synchronous, no automatic
I have two tables: 1) BlogAlbum: Id AlbumName CoverPhoto CreatedDate Description 1 Onam noImage.jpg
I have a wierd issue with jQuery which I've never seen before. I have
This was a very bizarre scenario I just witnessed on a colleague's machine. While
I currently have mirroring setup between three computers, principle, mirror, and witness. During the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.