develooper Front page | perl.perl6.users | Postings from February 2019

Re: reassigning values to variables question

Thread Previous | Thread Next
From:
ToddAndMargo via perl6-users
Date:
February 6, 2019 04:49
Subject:
Re: reassigning values to variables question
Message ID:
197a6a97-f5ce-4e1d-9d9a-3feefe406229@zoho.com
>> 
>> 
>> On Sun, Feb 3, 2019 at 9:36 PM ToddAndMargo via perl6-users 
>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>> 
>>     Hi All,
>> 
>>     If I have a variable of type Buf which 10000000 bytes in it
>>     and I find the five bytes I want, is it faster, slower,
>>     or no difference in speed to overwrite the same variable
>>     with the five bytes?  Or is it faster to put the five bytes
>>     from the first variable into a second variable?
>> 
>>     Many thanks,
>>     -T
>> 

On 2/5/19 8:42 AM, yary wrote:
> There are modules to time two pieces of code and show the difference
> https://github.com/perl6-community-modules/perl6-Benchy
> https://github.com/tony-o/perl6-bench
> 
> You can write up the two versions you're thinking of, feed them to the 
> benchmark module, and show us what you find!
> 
> -y


Hi Yary,

Thank you!

Apparently, overwriting the original buffer only change the
structures pointers, which is almost instantaneous.

And you taught me something new today!

-T


<code VarTest.pl6>

#!/usr/bin/env perl6

use Bench;

my IO::Handle $HaystackHandle = open( "/home/temp/procexp64.exe", :bin, 
:ro );
my Buf $Haystack              = $HaystackHandle.read( 3000000 );
$HaystackHandle.close;

my Buf $Needle;

my $b = Bench.new;

sub Another() { $Needle   = $Haystack.subbuf( 0x14FFAC .. 0x145FAC ); }
sub Same()    { $Haystack = $Haystack.subbuf( 0x14FFAC .. 0x145FAC ); }

say "first copies to a new variable; second overwrites the same variable";
$b.timethese( 100000, {
   first  => sub { Another; },
   second => sub { Same; },
});

</code VarTest.pl6>


$ VarTest.pl6
first copies to a new variable; second overwrites the same variable
Benchmark:
Timing 100000 iterations of first, second...
      first: 0.021 wallclock secs (0.021 usr 0.000 sys 0.021 cpu) @ 
4676612.262/s (n=100000)
     second: 0.000 wallclock secs (0.000 usr 0.000 sys 0.000 cpu)

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About