Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: New software TRNG

by GrandFather (Saint)
on Jan 30, 2014 at 01:14 UTC ( [id://1072610]=note: print w/replies, xml ) Need Help??


in reply to Re^2: New software TRNG
in thread New software TRNG

Update: Argh! tye is right. My understanding of bignum was flawed - bignum doesn't work like most modules and even a detailed read of the documentation doesn't make that obvious. As tim.qfs points out in his reply changing where the 'use bignum;' statement is does alter the code generated at compile time.

But you still don't understand the compile/execute nature of Perl script execution and how that affects 'use' statements. All the use statements are executed before any of the script is executed (except where BEGIN blocks are used, but don't go there until you really know what you are doing). Moving the 'use' statements around does not affect the fact that modules loaded using 'use' are loaded before the script executes.

There are several ways to fix that. One way would be to put your sub in a module and have it do the number generation when loaded. Another way is to use require to load bignum. Best of all is to identify the nasty interaction and fix your code so that the presence of bignum doesn't matter. It would be interesting to know what the effect of bignum is on your code as that may point to a weakness in your code that will bite in other contexts.

If the code changes take longer than the time saved, it's fast enough already.

Replies are listed 'Best First'.
Re^4: New software TRNG (compile)
by tye (Sage) on Jan 30, 2014 at 02:39 UTC
    But you still don't understand the compile/execute nature of Perl script execution and how that affects 'use' statements. [....] Moving the 'use' statements around does not affect the fact that modules loaded using 'use' are loaded before the script executes.

    And you seem to not understand the purpose of bignum.pm.

    (At least by default) bignum.pm overloads the handling of numeric constants when compiling Perl code so you get Math::BigFloat or Math::BigInt or similar objects in place of each numeric constant.

    So what matters is whether or not bignum.pm is loaded before or after whatever part of the script is compiled, not executed.

    BTW, a much saner approach is to just use whichever Math::Big* or similar module directly and only resort to huge (and slow) numbers in the precise cases where you need to.

    - tye        

Re^4: New software TRNG
by tim.qfs (Novice) on Jan 30, 2014 at 01:41 UTC
    From repeated experiment I have found out that if I put "use bignum;" before the subroutine, it breaks it but not if I put it after the subroutine.

    The interaction is that bignum or one of the modules that bignum uses slows down the central until-loop so that it is many times longer than eight microseconds. This breaks it because the subroutine is supposed to measure the number of central until-loop cycles in eight microseconds. I think that bignum slows down the maths involved.

      That make sense and is much what I suspected. Leaving aside the question of what that says about the robustness of your TRNG, a better solution to the problem is to ensure bignum is "off" in your sub:

      sub TRNGrand { no bignum; ... }
      If the code changes take longer than the time saved, it's fast enough already.
        Ahhhh! Thank you very much indeed for that. I looked all over the Internet for a way of switching off modules in subroutines but found nothing!

        I have tried it out and it works. I can now completely get rid of the comments regarding bignum.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found