Results 1 to 17 of 17

Thread: for loops in c++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: for loops in c++

    Quote Originally Posted by stinos View Post
    well I'm nut sure whether or not this is representative; I ran each test a couple of times and each time the result was more or less the same (times in mSec btw).
    The fact that you can reproduce it doesn't mean it's representative.

    Can't I conclude then, that for these particular pieces of code, the second one is noticably faster than the first one, though they both yield the same result?
    Sure, it's faster. Noticably faster - I'm not sure. Biased to prove the point - definitely so

    It is true that post-increment is in most cases slower than pre-increment, but the for() loop has nothing to do with it, so let's not exagerrate it - we mostly iterate fors using integers.

    how do I measure the process time btw?
    Using clock(), times() or "time" shell command. Or a profiler.

  2. #2
    Join Date
    Sep 2006
    Posts
    27
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 2 Times in 1 Post

    Default Re: for loops in c++

    Quote Originally Posted by wysota View Post
    The fact that you can reproduce it doesn't mean it's representative.
    mmm, good point


    Biased to prove the point - definitely so
    yeah I know, I'm a bit too keen on details sometimes..


    Using clock(), times() or "time" shell command. Or a profiler.
    I'm afraid I don't understand this: what's the difference between using QueryPerformanceCounter() or rdtsc() and using eg clock()? Each returns a timer value, possibly from different timers, but how else then measurig a start and stop time and subtracting the two can one mesure execution time? Doesn't a profiler work like that?
    Suppose I use time() on the shell, wouldn't time at process stop - time at process start be equal to the two time values I measure inside the test() function + time needed for program init etc?

    @TS: sorry for the off-topic discussion, but on the other hand, we'll probably learn from it, seems to me wysota is rather experienced!

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: for loops in c++

    Quote Originally Posted by stinos View Post
    Each returns a timer value, possibly from different timers, but how else then measurig a start and stop time and subtracting the two can one mesure execution time?
    clock(), time() and "time" ask the system (scheduler) to provide information how much cpu time was spent on the process, not how many miliseconds have elapsed between starting and finishing the execution.

    Doesn't a profiler work like that?
    Depends what it does. The number of cpu instructions executed is independent of time. If a profiler only measures world time elapsed, then it's biased as well.

    Suppose I use time() on the shell, wouldn't time at process stop - time at process start be equal to the two time values I measure inside the test() function + time needed for program init etc?
    No, because I can stop the process (CTRL+Z) and restart it two days later ("fg" or "bg"). time will give me a proper result (like 20ms), substracting end and start times will give me 2 days and 20ms. The process can be preemptied practically at any time to yield the CPU to some other process which makes results of such primitive calculations incorrect. The difference between those two measurements is "how much time it took to execute the code" and "how much time it took to execute the code in particular conditions" where "particular conditions" are impossible to recreate, so the measurement is not representative.

  4. #4
    Join Date
    Sep 2006
    Posts
    27
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 2 Times in 1 Post

    Default Re: for loops in c++

    Quote Originally Posted by wysota View Post
    clock(), time() and "time" ask the system (scheduler) to provide information how much cpu time was spent on the process, not how many miliseconds have elapsed between starting and finishing the execution.
    thanks for clearing that out! I really didn't know that.

  5. #5
    Join Date
    Sep 2006
    Posts
    27
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1
    Thanked 2 Times in 1 Post

    Default Re: for loops in c++

    Quote Originally Posted by wysota View Post
    The process can be preemptied practically at any time to yield the CPU to some other process which makes results of such primitive calculations incorrect. The difference between those two measurements is "how much time it took to execute the code" and "how much time it took to execute the code in particular conditions" where "particular conditions" are impossible to recreate, so the measurement is not representative.
    I've been thinking about this some more: does this mean that using clock() would, for a program like the above, always return the same results, no matter what the system load is?
    There's no sleep or waiting conditions, only one thread, so the code executed should always follow the exact same path, so using clock() in the places where I used QueryPerformanceCounter() should always return the exact same value, or is there a pitfall of some kind?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: for loops in c++

    Quote Originally Posted by stinos View Post
    I've been thinking about this some more: does this mean that using clock() would, for a program like the above, always return the same results, no matter what the system load is?
    It is approximate so the exact values might vary a bit, but in general - yes, at least I think so.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.