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 08:44
Subject:
Re: reassigning values to variables question
Message ID:
687d137f-e05c-1889-3032-6b8f3688ec7c@zoho.com
On 2/5/19 11:56 PM, ToddAndMargo via perl6-users wrote:
> On 2/5/19 8:34 PM, ToddAndMargo via perl6-users wrote:
>>>>
>>>>
>>>> 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)
> 
> 
> You guys catch my mistake?  It is only valid for one iteration.
> Chuckle.  A rewrite is in order.


<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 $SubBuf;
my $b = Bench.new;

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

say "first copies to a new variable; second overwrites the same variable";
$b.timethese( 100000, {
   first  => sub { $SubBuf = Another(); },
   second => sub { $SubBuf = 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.051 wallclock secs (0.054 usr 0.003 sys 0.057 cpu) @ 
1965370.177/s (n=100000)
     second: 0.052 wallclock secs (0.045 usr 0.001 sys 0.046 cpu) @ 
1926856.526/s (n=100000)


Virtually no difference

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