int(rand() + 1), int(rand()) +1, and hashes

Eugene Tsyrklevich eugene at securityarchitects.com
Sun Jul 16 11:15:52 CDT 2000


~sdpm~
On Sun, Jul 16, 2000 at 08:08:35AM -0700, C. Abney wrote:
> ~sdpm~
> On Sun, 16 Jul 2000, Russ Schnapp wrote:
> 
> > Okay, I'll bite.  Here are the code samples again:
> > 
> > > sub randint1 {
> > >         $r1 = int( rand(4) + 1 );
> > >         $h1{$r1}++;
> > > }
> > > 
> > > sub randint2 {
> > >         $r2 = int( rand(4) ) + 1;
> > >         $h2{$r2}++;
> > > }
> > 
> > randint2() ran slower than randing1(), and you're saying that's 
> > because $r2 was a float while $r1 was an int?  I need to understand 
> > this.  How did adding an integer (1) to the result of an int() generate 
> > a floating result?
> 
> oh my, Eugene seems to've missed something in my post. $r1 and $r2 are
> both ints.  It is in the assignment operation for each that the output
> of rand() is converted from float to int.  This happens /before/ it's
> used as a hash key, of course.  In one example it happens before adding
> 1, in the other example it happens after.  That's the only difference
> other than speed.
> 
> Things are only getting muddier.  Both randint1 and randint2 use the
> hash, and the hash index has been converted from an int in both cases.
> 
> But, in one case, rand() output was truncated to int before integer
> addition and assignment to $rx, whereas in the other it was added with
> an int /before/ truncation.  The second case turns out to be much faster
> when the resulting int is used as a hash index.  But it makes no difference
> in speed when the resulting ints are used as an array index!
> 
> I want to get inside that anomaly and am hoping for some help... :)
> Maybe it would help to review the benchmark again, comparing the 2x2
> matrix of algorithms?


bash-2.03# cat a.pl
#!/usr/bin/perl

use Devel::Peek;

$a=int( rand(4) + 1);
$b=int( rand(4) ) + 1;

Dump($a);
Dump($b);



bash-2.03# perl a.pl
SV = IV(0xf4b0) at 0x6ef4
  REFCNT = 1
  FLAGS = (IOK,pIOK)			<- int value
  IV = 2
SV = NV(0x14820) at 0x6f24
  REFCNT = 1
  FLAGS = (NOK,pNOK)			<- floating point value
  NV = 1


my argument still stands.


doesn't make sense right? i mean both number are supposed to be integers..
here is another example for ya


bash-2.03# cat a.pl
#!/usr/bin/perl

use Devel::Peek;

$a = 1;
$a += 1;

Dump($a);



bash-2.03# perl a.pl
SV = PVNV(0x335e0) at 0x6ef4
  REFCNT = 1
  FLAGS = (NOK,pNOK)			<- floating point
  IV = 1
  NV = 2
  PV = 0


perl converted our interger to a floating point before performing our
arithmetic operation.
~sdpm~

The posting address is: san-diego-pm-list at hfb.pm.org

List requests should be sent to: majordomo at hfb.pm.org

If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at happyfunball.pm.org> with the following
command in the body of your email message:

    unsubscribe san-diego-pm-list

If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at happyfunball.pm.org> .
This is the general rule for most mailing lists when you need
to contact a human.




More information about the San-Diego-pm mailing list