Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Near equal partitions.

by ELISHEVA (Prior)
on Jan 14, 2011 at 05:44 UTC ( #882290=note: print w/replies, xml ) Need Help??


in reply to Re: Near equal partitions.
in thread Near equal partitions.

That list generation is lovely!

Just one question. Is the Perl interpreter smart enough to know that int($n/$m) should use integer division rather than floating point division? i.e. would there be any difference in performance between int($n/$m) vs. ($n-$r)/$m?

In more strongly typed languages, the type of the variables determines the meaning of the '/' operator (integer division vs. floating point division) so ($n-$r)/$m would be faster. However in Perl, the operator determines the type of the operands, so maybe there is no difference in performance between the two expressions in Perl? Or maybe ($n-$r)/$m is slower because the int(...) gives the Perl interpreter a clue to context and hence the opportunity to use integer division when it sees such a simple expression as int( token / token )?

Replies are listed 'Best First'.
Re^3: Near equal partitions.
by ikegami (Pope) on Jan 14, 2011 at 05:53 UTC

    Just one question. Is the Perl interpreter smart enough to know that int($n/$m) should use integer division rather than floating point division?

    No.

    i.e. would there be any difference in performance betwee int($n/$m) vs. ($n-$r)/$m?

    Sorry, you asked to ask two questions. But to save you from having to ask to ask another question, I'll answer it anyway.

    Performance should be roughly the same (4 trivial ops in the former, 5 trivial ops in the latter). The latter is better from a numerical methods point of view, no possibility of floating point error.

      btw, what about use integer? It looks it's little bit faster with that.
        There's benchmarking code elsewhere in the thread. It should be easy to add a 'use integer' case.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://882290]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2020-01-21 22:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?