reassigning values to variables question

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
0
perl6
2/4/2019 5:35:43 AM
perl.perl6.users 1200 articles. 0 followers. Follow

6 Replies
79 Views

Similar Articles

[PageSpeed] 57

--0000000000000a86ca0581284e08
Content-Type: text/plain; charset="UTF-8"

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


On Sun, Feb 3, 2019 at 9:36 PM ToddAndMargo via perl6-users <
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
>

--0000000000000a86ca0581284e08
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr">There are modules to tim=
e two pieces of code and show the difference<div><a href=3D"https://github.=
com/perl6-community-modules/perl6-Benchy">https://github.com/perl6-communit=
y-modules/perl6-Benchy</a></div><div><a href=3D"https://github.com/tony-o/p=
erl6-bench">https://github.com/tony-o/perl6-bench</a></div><div><br></div><=
div>You can write up the two versions you&#39;re thinking of, feed them to =
the benchmark module, and show us what you find!</div><div><br></div><div><=
div><div dir=3D"ltr" class=3D"gmail_signature">-y<br></div></div><br></div>=
</div></div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"=
gmail_attr">On Sun, Feb 3, 2019 at 9:36 PM ToddAndMargo via perl6-users &lt=
;<a href=3D"mailto:perl6-users@perl.org">perl6-users@perl.org</a>&gt; wrote=
:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.=
8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204=
,204,204);padding-left:1ex">Hi All,<br>
<br>
If I have a variable of type Buf which 10000000 bytes in it<br>
and I find the five bytes I want, is it faster, slower,<br>
or no difference in speed to overwrite the same variable<br>
with the five bytes?=C2=A0 Or is it faster to put the five bytes<br>
from the first variable into a second variable?<br>
<br>
Many thanks,<br>
-T<br>
</blockquote></div>

--0000000000000a86ca0581284e08--
0
not
2/5/2019 4:42:56 PM
>> 
>> 
>> 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)
0
perl6
2/6/2019 4:34:11 AM
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=20
>>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>>>
>>> =C2=A0=C2=A0=C2=A0 Hi All,
>>>
>>> =C2=A0=C2=A0=C2=A0 If I have a variable of type Buf which 10000000 by=
tes in it
>>> =C2=A0=C2=A0=C2=A0 and I find the five bytes I want, is it faster, sl=
ower,
>>> =C2=A0=C2=A0=C2=A0 or no difference in speed to overwrite the same va=
riable
>>> =C2=A0=C2=A0=C2=A0 with the five bytes?=C2=A0 Or is it faster to put =
the five bytes
>>> =C2=A0=C2=A0=C2=A0 from the first variable into a second variable?
>>>
>>> =C2=A0=C2=A0=C2=A0 Many thanks,
>>> =C2=A0=C2=A0=C2=A0 -T
>>>
>=20
> 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=
=20
>> benchmark module, and show us what you find!
>>
>> -y
>=20
>=20
> Hi Yary,
>=20
> Thank you!
>=20
> Apparently, overwriting the original buffer only change the
> structures pointers, which is almost instantaneous.
>=20
> And you taught me something new today!
>=20
> -T
>=20
>=20
> <code VarTest.pl6>
>=20
> #!/usr/bin/env perl6
>=20
> use Bench;
>=20
> my IO::Handle $HaystackHandle =3D open( "/home/temp/procexp64.exe", :bi=
n,=20
> :ro );
> my Buf $Haystack=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 =3D $HaystackHandle.read( 3000000 );
> $HaystackHandle.close;
>=20
> my Buf $Needle;
>=20
> my $b =3D Bench.new;
>=20
> sub Another() { $Needle=C2=A0=C2=A0 =3D $Haystack.subbuf( 0x14FFAC .. 0=
x145FAC ); }
> sub Same()=C2=A0=C2=A0=C2=A0 { $Haystack =3D $Haystack.subbuf( 0x14FFAC=
 .. 0x145FAC ); }
>=20
> say "first copies to a new variable; second overwrites the same variabl=
e";
> $b.timethese( 100000, {
>  =C2=A0 first=C2=A0 =3D> sub { Another; },
>  =C2=A0 second =3D> sub { Same; },
> });
>=20
> </code VarTest.pl6>
>=20
>=20
> $ VarTest.pl6
> first copies to a new variable; second overwrites the same variable
> Benchmark:
> Timing 100000 iterations of first, second...
>  =C2=A0=C2=A0=C2=A0=C2=A0 first: 0.021 wallclock secs (0.021 usr 0.000 =
sys 0.021 cpu) @=20
> 4676612.262/s (n=3D100000)
>  =C2=A0=C2=A0=C2=A0 second: 0.000 wallclock secs (0.000 usr 0.000 sys 0=
=2E000 cpu)


You guys catch my mistake?  It is only valid for one iteration.
Chuckle.  A rewrite is in order.
0
perl6
2/6/2019 7:56:09 AM
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=20
>>>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>>>>
>>>> =C2=A0=C2=A0=C2=A0 Hi All,
>>>>
>>>> =C2=A0=C2=A0=C2=A0 If I have a variable of type Buf which 10000000 b=
ytes in it
>>>> =C2=A0=C2=A0=C2=A0 and I find the five bytes I want, is it faster, s=
lower,
>>>> =C2=A0=C2=A0=C2=A0 or no difference in speed to overwrite the same v=
ariable
>>>> =C2=A0=C2=A0=C2=A0 with the five bytes?=C2=A0 Or is it faster to put=
 the five bytes
>>>> =C2=A0=C2=A0=C2=A0 from the first variable into a second variable?
>>>>
>>>> =C2=A0=C2=A0=C2=A0 Many thanks,
>>>> =C2=A0=C2=A0=C2=A0 -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=20
>>> 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 =3D open( "/home/temp/procexp64.exe",=20
>> :bin, :ro );
>> my Buf $Haystack=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0 =3D $HaystackHandle.read( 3000000 );
>> $HaystackHandle.close;
>>
>> my Buf $Needle;
>>
>> my $b =3D Bench.new;
>>
>> sub Another() { $Needle=C2=A0=C2=A0 =3D $Haystack.subbuf( 0x14FFAC .. =
0x145FAC ); }
>> sub Same()=C2=A0=C2=A0=C2=A0 { $Haystack =3D $Haystack.subbuf( 0x14FFA=
C .. 0x145FAC ); }
>>
>> say "first copies to a new variable; second overwrites the same=20
>> variable";
>> $b.timethese( 100000, {
>> =C2=A0=C2=A0 first=C2=A0 =3D> sub { Another; },
>> =C2=A0=C2=A0 second =3D> 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...
>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 first: 0.021 wallclock secs (0.021 usr =
0.000 sys 0.021 cpu) @=20
>> 4676612.262/s (n=3D100000)
>> =C2=A0=C2=A0=C2=A0=C2=A0 second: 0.000 wallclock secs (0.000 usr 0.000=
 sys 0.000 cpu)
>=20
>=20
> You guys catch my mistake?=C2=A0 It is only valid for one iteration.
> Chuckle.=C2=A0 A rewrite is in order.


<code VarTest.pl6>
#!/usr/bin/env perl6

use Bench;

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

my Buf $SubBuf;
my $b =3D Bench.new;

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

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


Virtually no difference
0
perl6
2/6/2019 8:44:29 AM
On 2/6/19 12:44 AM, ToddAndMargo via perl6-users wrote:
> 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=20
>>>>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>>>>>
>>>>> =C2=A0=C2=A0=C2=A0 Hi All,
>>>>>
>>>>> =C2=A0=C2=A0=C2=A0 If I have a variable of type Buf which 10000000 =
bytes in it
>>>>> =C2=A0=C2=A0=C2=A0 and I find the five bytes I want, is it faster, =
slower,
>>>>> =C2=A0=C2=A0=C2=A0 or no difference in speed to overwrite the same =
variable
>>>>> =C2=A0=C2=A0=C2=A0 with the five bytes?=C2=A0 Or is it faster to pu=
t the five bytes
>>>>> =C2=A0=C2=A0=C2=A0 from the first variable into a second variable?
>>>>>
>>>>> =C2=A0=C2=A0=C2=A0 Many thanks,
>>>>> =C2=A0=C2=A0=C2=A0 -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=20
>>>> 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 =3D open( "/home/temp/procexp64.exe",=20
>>> :bin, :ro );
>>> my Buf $Haystack=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0 =3D $HaystackHandle.read( 3000000 );
>>> $HaystackHandle.close;
>>>
>>> my Buf $Needle;
>>>
>>> my $b =3D Bench.new;
>>>
>>> sub Another() { $Needle=C2=A0=C2=A0 =3D $Haystack.subbuf( 0x14FFAC ..=
 0x145FAC ); }
>>> sub Same()=C2=A0=C2=A0=C2=A0 { $Haystack =3D $Haystack.subbuf( 0x14FF=
AC .. 0x145FAC ); }
>>>
>>> say "first copies to a new variable; second overwrites the same=20
>>> variable";
>>> $b.timethese( 100000, {
>>> =C2=A0=C2=A0 first=C2=A0 =3D> sub { Another; },
>>> =C2=A0=C2=A0 second =3D> 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...
>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 first: 0.021 wallclock secs (0.021 usr=
 0.000 sys 0.021 cpu) @=20
>>> 4676612.262/s (n=3D100000)
>>> =C2=A0=C2=A0=C2=A0=C2=A0 second: 0.000 wallclock secs (0.000 usr 0.00=
0 sys 0.000 cpu)
>>
>>
>> You guys catch my mistake?=C2=A0 It is only valid for one iteration.
>> Chuckle.=C2=A0 A rewrite is in order.
>=20
>=20
> <code VarTest.pl6>
> #!/usr/bin/env perl6
>=20
> use Bench;
>=20
> my IO::Handle $HaystackHandle =3D open( "/home/temp/procexp64.exe", :bi=
n,=20
> :ro );
> my Buf $Haystack=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 =3D $HaystackHandle.read( 3000000 );
> $HaystackHandle.close;
>=20
> my Buf $SubBuf;
> my $b =3D Bench.new;
>=20
> sub Another() { my Buf $b =3D $Haystack; my Buf $Needle =3D $b.subbuf( =

> 0x14FFAC .. 0x145FAC ); }
> sub Same()=C2=A0=C2=A0=C2=A0 { my Buf $b =3D $Haystack; $b =3D $b.subbu=
f( 0x14FFAC ..=20
> 0x145FAC ); }
>=20
> say "first copies to a new variable; second overwrites the same variabl=
e";
> $b.timethese( 100000, {
>  =C2=A0 first=C2=A0 =3D> sub { $SubBuf =3D Another(); },
>  =C2=A0 second =3D> sub { $SubBuf =3D Same(); },
> });
> </code VarTest.pl6>
>=20
>=20
>=20
> $ VarTest.pl6
> first copies to a new variable; second overwrites the same variable
> Benchmark:
> Timing 100000 iterations of first, second...
>  =C2=A0=C2=A0=C2=A0=C2=A0 first: 0.051 wallclock secs (0.054 usr 0.003 =
sys 0.057 cpu) @=20
> 1965370.177/s (n=3D100000)
>  =C2=A0=C2=A0=C2=A0 second: 0.052 wallclock secs (0.045 usr 0.001 sys 0=
=2E046 cpu) @=20
> 1926856.526/s (n=3D100000)
>=20
>=20
> Virtually no difference

And it would help if I did not reuse variable names.

A little bit of difference:

New variable =3D 0.053 wallclock secs
Overwrite    =3D 0.085 wallclock secs


<code VarTest.pl6>
#!/usr/bin/env perl6

use Bench;

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

my Buf $SubBuf;
my $b =3D Bench.new;

sub Another() { my Buf $bb =3D $Haystack; my Buf $Needle =3D $bb.subbuf( =

0x14FFAC .. 0x145FAC ); }
sub Same()    { my Buf $bb =3D $Haystack; $bb =3D $bb.subbuf( 0x14FFAC ..=
=20
0x145FAC ); }

say "first copies to a new variable; second overwrites the same variable"=
;
$b.timethese( 100000, {
   first  =3D> sub { $SubBuf =3D Another(); },
   second =3D> sub { $SubBuf =3D 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.053 wallclock secs (0.052 usr 0.003 sys 0.055 cpu) @=20
1899696.049/s (n=3D100000)
     second: 0.085 wallclock secs (0.078 usr 0.000 sys 0.078 cpu) @=20
1180442.430/s (n=3D100000)




--=20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
perl6
2/6/2019 8:50:18 AM
--0000000000005043ad05813bf52a
Content-Type: text/plain; charset="UTF-8"

Hope that was as fun to write, as it was for me to read! Thanks.
On Wed, Feb 6, 2019 at 12:50 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2/6/19 12:44 AM, ToddAndMargo via perl6-users wrote:
> > 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
>
> And it would help if I did not reuse variable names.
>
> A little bit of difference:
>
> New variable = 0.053 wallclock secs
> Overwrite    = 0.085 wallclock secs
>
>
> <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 $bb = $Haystack; my Buf $Needle = $bb.subbuf(
> 0x14FFAC .. 0x145FAC ); }
> sub Same()    { my Buf $bb = $Haystack; $bb = $bb.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.053 wallclock secs (0.052 usr 0.003 sys 0.055 cpu) @
> 1899696.049/s (n=100000)
>      second: 0.085 wallclock secs (0.078 usr 0.000 sys 0.078 cpu) @
> 1180442.430/s (n=100000)
>
>
>
>
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Computers are like air conditioners.
> They malfunction when you open windows
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>

--0000000000005043ad05813bf52a
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hope that was as fun to write, as it was for me to read! Thanks.<br><div cl=
ass=3D"gmail_quote"><div dir=3D"ltr">On Wed, Feb 6, 2019 at 12:50 AM ToddAn=
dMargo via perl6-users &lt;<a href=3D"mailto:perl6-users@perl.org">perl6-us=
ers@perl.org</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" styl=
e=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 2/6/=
19 12:44 AM, ToddAndMargo via perl6-users wrote:<br>
&gt; On 2/5/19 11:56 PM, ToddAndMargo via perl6-users wrote:<br>
&gt;&gt; On 2/5/19 8:34 PM, ToddAndMargo via perl6-users wrote:<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; On Sun, Feb 3, 2019 at 9:36 PM ToddAndMargo via perl6-=
users <br>
&gt;&gt;&gt;&gt;&gt; &lt;<a href=3D"mailto:perl6-users@perl.org" target=3D"=
_blank">perl6-users@perl.org</a> &lt;mailto:<a href=3D"mailto:perl6-users@p=
erl.org" target=3D"_blank">perl6-users@perl.org</a>&gt;&gt; wrote:<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; =C2=A0=C2=A0=C2=A0 Hi All,<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; =C2=A0=C2=A0=C2=A0 If I have a variable of type Buf wh=
ich 10000000 bytes in it<br>
&gt;&gt;&gt;&gt;&gt; =C2=A0=C2=A0=C2=A0 and I find the five bytes I want, i=
s it faster, slower,<br>
&gt;&gt;&gt;&gt;&gt; =C2=A0=C2=A0=C2=A0 or no difference in speed to overwr=
ite the same variable<br>
&gt;&gt;&gt;&gt;&gt; =C2=A0=C2=A0=C2=A0 with the five bytes?=C2=A0 Or is it=
 faster to put the five bytes<br>
&gt;&gt;&gt;&gt;&gt; =C2=A0=C2=A0=C2=A0 from the first variable into a seco=
nd variable?<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; =C2=A0=C2=A0=C2=A0 Many thanks,<br>
&gt;&gt;&gt;&gt;&gt; =C2=A0=C2=A0=C2=A0 -T<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; On 2/5/19 8:42 AM, yary wrote:<br>
&gt;&gt;&gt;&gt; There are modules to time two pieces of code and show the =
difference<br>
&gt;&gt;&gt;&gt; <a href=3D"https://github.com/perl6-community-modules/perl=
6-Benchy" rel=3D"noreferrer" target=3D"_blank">https://github.com/perl6-com=
munity-modules/perl6-Benchy</a><br>
&gt;&gt;&gt;&gt; <a href=3D"https://github.com/tony-o/perl6-bench" rel=3D"n=
oreferrer" target=3D"_blank">https://github.com/tony-o/perl6-bench</a><br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; You can write up the two versions you&#39;re thinking of, =
feed them to <br>
&gt;&gt;&gt;&gt; the benchmark module, and show us what you find!<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; -y<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Hi Yary,<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Thank you!<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Apparently, overwriting the original buffer only change the<br=
>
&gt;&gt;&gt; structures pointers, which is almost instantaneous.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; And you taught me something new today!<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; -T<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; &lt;code VarTest.pl6&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; #!/usr/bin/env perl6<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; use Bench;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; my IO::Handle $HaystackHandle =3D open( &quot;/home/temp/proce=
xp64.exe&quot;, <br>
&gt;&gt;&gt; :bin, :ro );<br>
&gt;&gt;&gt; my Buf $Haystack=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D $HaystackHandle.read( 3000000 );<br>
&gt;&gt;&gt; $HaystackHandle.close;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; my Buf $Needle;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; my $b =3D Bench.new;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; sub Another() { $Needle=C2=A0=C2=A0 =3D $Haystack.subbuf( 0x14=
FFAC .. 0x145FAC ); }<br>
&gt;&gt;&gt; sub Same()=C2=A0=C2=A0=C2=A0 { $Haystack =3D $Haystack.subbuf(=
 0x14FFAC .. 0x145FAC ); }<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; say &quot;first copies to a new variable; second overwrites th=
e same <br>
&gt;&gt;&gt; variable&quot;;<br>
&gt;&gt;&gt; $b.timethese( 100000, {<br>
&gt;&gt;&gt; =C2=A0=C2=A0 first=C2=A0 =3D&gt; sub { Another; },<br>
&gt;&gt;&gt; =C2=A0=C2=A0 second =3D&gt; sub { Same; },<br>
&gt;&gt;&gt; });<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; &lt;/code VarTest.pl6&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; $ VarTest.pl6<br>
&gt;&gt;&gt; first copies to a new variable; second overwrites the same var=
iable<br>
&gt;&gt;&gt; Benchmark:<br>
&gt;&gt;&gt; Timing 100000 iterations of first, second...<br>
&gt;&gt;&gt; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 first: 0.021 wallclock secs (0.=
021 usr 0.000 sys 0.021 cpu) @ <br>
&gt;&gt;&gt; 4676612.262/s (n=3D100000)<br>
&gt;&gt;&gt; =C2=A0=C2=A0=C2=A0=C2=A0 second: 0.000 wallclock secs (0.000 u=
sr 0.000 sys 0.000 cpu)<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; You guys catch my mistake?=C2=A0 It is only valid for one iteratio=
n.<br>
&gt;&gt; Chuckle.=C2=A0 A rewrite is in order.<br>
&gt; <br>
&gt; <br>
&gt; &lt;code VarTest.pl6&gt;<br>
&gt; #!/usr/bin/env perl6<br>
&gt; <br>
&gt; use Bench;<br>
&gt; <br>
&gt; my IO::Handle $HaystackHandle =3D open( &quot;/home/temp/procexp64.exe=
&quot;, :bin, <br>
&gt; :ro );<br>
&gt; my Buf $Haystack=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0 =3D $HaystackHandle.read( 3000000 );<br>
&gt; $HaystackHandle.close;<br>
&gt; <br>
&gt; my Buf $SubBuf;<br>
&gt; my $b =3D Bench.new;<br>
&gt; <br>
&gt; sub Another() { my Buf $b =3D $Haystack; my Buf $Needle =3D $b.subbuf(=
 <br>
&gt; 0x14FFAC .. 0x145FAC ); }<br>
&gt; sub Same()=C2=A0=C2=A0=C2=A0 { my Buf $b =3D $Haystack; $b =3D $b.subb=
uf( 0x14FFAC .. <br>
&gt; 0x145FAC ); }<br>
&gt; <br>
&gt; say &quot;first copies to a new variable; second overwrites the same v=
ariable&quot;;<br>
&gt; $b.timethese( 100000, {<br>
&gt;=C2=A0 =C2=A0 first=C2=A0 =3D&gt; sub { $SubBuf =3D Another(); },<br>
&gt;=C2=A0 =C2=A0 second =3D&gt; sub { $SubBuf =3D Same(); },<br>
&gt; });<br>
&gt; &lt;/code VarTest.pl6&gt;<br>
&gt; <br>
&gt; <br>
&gt; <br>
&gt; $ VarTest.pl6<br>
&gt; first copies to a new variable; second overwrites the same variable<br=
>
&gt; Benchmark:<br>
&gt; Timing 100000 iterations of first, second...<br>
&gt;=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 first: 0.051 wallclock secs (0.054 usr =
0.003 sys 0.057 cpu) @ <br>
&gt; 1965370.177/s (n=3D100000)<br>
&gt;=C2=A0 =C2=A0=C2=A0=C2=A0 second: 0.052 wallclock secs (0.045 usr 0.001=
 sys 0.046 cpu) @ <br>
&gt; 1926856.526/s (n=3D100000)<br>
&gt; <br>
&gt; <br>
&gt; Virtually no difference<br>
<br>
And it would help if I did not reuse variable names.<br>
<br>
A little bit of difference:<br>
<br>
New variable =3D 0.053 wallclock secs<br>
Overwrite=C2=A0 =C2=A0 =3D 0.085 wallclock secs<br>
<br>
<br>
&lt;code VarTest.pl6&gt;<br>
#!/usr/bin/env perl6<br>
<br>
use Bench;<br>
<br>
my IO::Handle $HaystackHandle =3D open( &quot;/home/temp/procexp64.exe&quot=
;, :bin, <br>
:ro );<br>
my Buf $Haystack=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =3D $Hayst=
ackHandle.read( 3000000 );<br>
$HaystackHandle.close;<br>
<br>
my Buf $SubBuf;<br>
my $b =3D Bench.new;<br>
<br>
sub Another() { my Buf $bb =3D $Haystack; my Buf $Needle =3D $bb.subbuf( <b=
r>
0x14FFAC .. 0x145FAC ); }<br>
sub Same()=C2=A0 =C2=A0 { my Buf $bb =3D $Haystack; $bb =3D $bb.subbuf( 0x1=
4FFAC .. <br>
0x145FAC ); }<br>
<br>
say &quot;first copies to a new variable; second overwrites the same variab=
le&quot;;<br>
$b.timethese( 100000, {<br>
=C2=A0 =C2=A0first=C2=A0 =3D&gt; sub { $SubBuf =3D Another(); },<br>
=C2=A0 =C2=A0second =3D&gt; sub { $SubBuf =3D Same(); },<br>
});<br>
&lt;/code VarTest.pl6&gt;<br>
<br>
<br>
<br>
$ VarTest.pl6<br>
first copies to a new variable; second overwrites the same variable<br>
Benchmark:<br>
Timing 100000 iterations of first, second...<br>
=C2=A0 =C2=A0 =C2=A0 first: 0.053 wallclock secs (0.052 usr 0.003 sys 0.055=
 cpu) @ <br>
1899696.049/s (n=3D100000)<br>
=C2=A0 =C2=A0 =C2=A0second: 0.085 wallclock secs (0.078 usr 0.000 sys 0.078=
 cpu) @ <br>
1180442.430/s (n=3D100000)<br>
<br>
<br>
<br>
<br>
-- <br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
Computers are like air conditioners.<br>
They malfunction when you open windows<br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
</blockquote></div>

--0000000000005043ad05813bf52a--
0
not
2/6/2019 4:09:50 PM
Reply: