in reply to notequal very slow

How are measuring this?

Is that line inside a loop?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: notequal very slow
by Anonymous Monk on Jan 15, 2008 at 20:42 UTC
    yes that line is inside a loop. I used a simple print statement to the console, just a line above the statement and that prints very fast. How ever a similar print statement to the console after the not equal statement seems to take a few seconds. Not sure what to do. I am pretty sure its correct code...I wouldnt know where to start looking...

      You need to profile your code. Going by how long print statements seem to take is about as useful as checking wind speed with a wetted finger. For a start, unless you have disabled output buffering, when you see output can be completely unrelated to when your program executed the print statement. In extreme cases, it could be displayed hours later.

      The reason for the loops question is that the statement contains two fairly heavily indirected references. In high iteration count tight loops it can make sense to remove invarient parts of such statements from the loop through the use of temporary variables, but without profiling, random tuning is pointless.

      It's like trying to improve the performance of your family saloon by polishing the paintwork, whilst ignoring the two hundred weight of junk in the boot and the roof rack.

      Grab Devel::Smallprof which will quickly and easily allow you to see exactly where any perfomance bottleneck exist, on a line-by-line basis.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      Cut the code down to just the loop, remove whatever you can, see if it's still slow. if it's not, the problem is in the code you removed. if it's still slow, post the remaining code here.

      update: what BrowserUk said, especially the part about output buffering: print statements are not generally a reliable way to measure timing (though you could include the current millisecond time, as given by Time::Hires in your debugging line, which should give you a reasonable indication), but even then, the if(...) statement you posted should probably take a lot less time to execute than printing a line to stdout.

      Print statements aren't an accurate way to measure performance. Try using Devel::SmallProf. It can be used from the command line like so perl -d:SmallProf your_script.pl. The output shows how often each line is executed and the amount of time it used.

      You could have buffering issues with your print. Did you turn on autoflushing on the handle to you which you print?