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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

When you reduce the overhead of the function calls (by doing the work you actually time in a tight loop), you might get other numbers

#!/pro/bin/perl -slw use 5.016; use warnings; use Benchmark qw{cmpthese :hireswallclock}; my $n = 0x80061861; say "and_rshift: $n => ", join (' : ', and_rshift ()); say "lshift_rshift: $n => ", join (' : ', lshift_rshift ()); say "mixed: $n => ", join (' : ', mixed ()); say "mixed_assign: $n => ", join (' : ', mixed_assign ()); say "just_shift: $n => ", join (' : ', just_shift ()); cmpthese (-10, { and_rshift => \&and_rshift, lshift_rshift => \&lshift_rshift, mixed => \&mixed, mixed_assign => \&mixed_assign, just_shift => \&just_shift, }); sub and_rshift { my ($top14, $nxt6, $mid6, $bot6); for (1..10000) { $top14 = ($n & 0xfffc0000) >> 18; $nxt6 = ($n & 0x0003f000) >> 12; $mid6 = ($n & 0x00000fc0) >> 6; $bot6 = ($n & 0x0000003f); } return ($top14, $nxt6, $mid6, $bot6); } sub lshift_rshift { my ($top14, $nxt6, $mid6, $bot6); for (1..10000) { $top14 = $n >> 18; $nxt6 = $n << 46 >> 58; $mid6 = $n << 52 >> 58; $bot6 = $n << 58 >> 58; } return ($top14, $nxt6, $mid6, $bot6); } sub mixed { my ($top14, $nxt6, $mid6, $bot6); for (1..10000) { ($top14, $nxt6, $mid6, $bot6) = ($n >> 18, $n >> 12 & 0x3f, $n >> 6 & 0x3f, $n & 0x3f); } return ($top14, $nxt6, $mid6, $bot6); } sub mixed_assign { my ($top14, $nxt6, $mid6, $bot6); for (1..10000) { $top14 = $n >> 18; $nxt6 = $n >> 12 & 0x3f; $mid6 = $n >> 6 & 0x3f; $bot6 = $n & 0x3f; } return ($top14, $nxt6, $mid6, $bot6); } sub just_shift { my ($top14, $nxt6, $mid6, $bot6); for (1..10000) { ($top14, $nxt6, $mid6, $bot6) = ($n >> 18, $n << 46 >> 58, $n << 52 >> 58, $n << 58 >> 58) +; } return ($top14, $nxt6, $mid6, $bot6); }
and_rshift: 2147883105 => 8193 : 33 : 33 : 33 lshift_rshift: 2147883105 => 8193 : 33 : 33 : 33 mixed: 2147883105 => 8193 : 33 : 33 : 33 mixed_assign: 2147883105 => 8193 : 33 : 33 : 33 just_shift: 2147883105 => 8193 : 33 : 33 : 33 Rate just_shift mixed mixed_assign and_rshift lsh +ift_rshift just_shift 247/s -- -2% -43% -48% + -52% mixed 252/s 2% -- -42% -47% + -52% mixed_assign 434/s 75% 72% -- -9% + -16% and_rshift 480/s 94% 90% 10% -- + -8% lshift_rshift 520/s 110% 106% 20% 8% + --

Enjoy, Have FUN! H.Merijn

In reply to Re^2: Efficient bit-twiddling in Perl. by Tux
in thread Efficient bit-twiddling in Perl. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found