Thread: Why is this faster

  1. #1
    Registered User Alienchicken's Avatar
    Join Date
    Feb 2009
    Posts
    22

    Why is this faster

    Hi everyone, I'm working on a program here and came across this discovery
    Code:
     
    for(i=0;i<Nli++){
     while(arr[i] * h[index[i]] <= h[num-1])  index[i]+=1;
    }
    AND

    Code:
    for(i=0;i<Nli++){
        index=0;
     while(arr[i] * h[index] <= h[num-1])  index++;
    }
    When i run both in my program, the compile time for the first code is 0 seconds however for the second code it takes 5 second.

    Can someone explain to me why is it that using index as an array is faster?

    Thanks
    Last edited by Alienchicken; 03-07-2011 at 10:11 PM. Reason: mistake

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Do you mean compile time or run time?
    Two different things!!

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You do realize these two code are entirely different.
    1st case, index is array. index[i]+=1;
    2nd , index is just an integer. perhaps? show the declaration!

  4. #4
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Ok I think in the second one index is the address of the array?
    Or is it?

    It would help if you printed the values of the variables ie

    arr[i], h[index] and [num-1]

    then you would have an idea of what is going on, maybe.

    I think I am agreeing with Bayint, 'index' is probably a huge 64 bit number such as 9347593475972

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I'm not surprised they take differing amounts of time. It looks to me like you've made a 'floptimisation'. It's not just slower, it doesn't actually do the same thing any more, and would clearly give different results.

    Learn the difference between compile-time and run-time.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is faster and is there any difference?
    By yann in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2010, 01:59 AM
  2. Faster bitwise operator
    By Yarin in forum C++ Programming
    Replies: 18
    Last Post: 04-29-2009, 01:56 PM
  3. Faster way of printing to the screen
    By cacophonix in forum C Programming
    Replies: 16
    Last Post: 02-04-2009, 01:18 PM
  4. does const make functions faster?
    By MathFan in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2005, 09:03 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM