Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

accessing perl's internal hashing algorithm
Hi, I have a performance intensive script that needs to hash a lot of
strings.  I am guessing that perl's internal hashing algorithm is
implemented at a low level, is optimized, etc. and I would like to
access it though my perl scripts. Does anyone know how to do this?

Thanks,
Rob

Report this thread to moderator Post Follow-up to this message
Old Post
rob
09-27-04 09:01 PM


Re: accessing perl's internal hashing algorithm
rob wrote:
> Hi, I have a performance intensive script that needs to hash a lot of
> strings.  I am guessing that perl's internal hashing algorithm is
> implemented at a low level, is optimized, etc. and I would like to
> access it though my perl scripts. Does anyone know how to do this?
I wouldn't have thought that the algorithm would be suitable for general
purpose use, but I stand open to correction. The Digest::SHA1 and
Digest::MD5 modules, for example, are implemented via xs and not native
perl (though clearly this is not a guarantee of speed: the algorithm can
suck whatever the implementation), so if raw performance is your
requirement then you could do worse than starting with these. You could
always check out the perl source directly if you're stuck on using
perl's internal algorithm: hv.c?

Mark

Report this thread to moderator Post Follow-up to this message
Old Post
Mark Clements
09-27-04 09:01 PM


Re: accessing perl's internal hashing algorithm
rob wrote:

> Hi, I have a performance intensive script that needs to hash a lot of
> strings.  I am guessing that perl's internal hashing algorithm is
> implemented at a low level, is optimized, etc. and I would like to
> access it though my perl scripts. Does anyone know how to do this?

A lot of the performance hit is probably due to Perl dynamically
enlarging hashes. If I remember correctly, when you declare a hash, Perl
allocates a certain size for it. As you starting populating this hash,
and once the number of keys hashed to the same integer exceeds a certain
threshold, Perl doubles the size of the hash (implemented as an array in
C), and re-indexes the keys. This can cause a lot of performace delay in
the case of a large number of keys to be hashed, especially if this
needs to be done multiple times.

The solution is to preallocate a large enough hash, if you think you can
guess how many keys you will need:

keys %hash = 5000;

Perl will round that number to the next power of two.

All of this is explained in Srinivasan's "Advanced Perl Programming"
(the Panther).

--Ala

Report this thread to moderator Post Follow-up to this message
Old Post
Ala Qumsieh
09-27-04 09:01 PM


Re: accessing perl's internal hashing algorithm
robert_parks@csgsystems.com (rob) wrote:
> Hi, I have a performance intensive script that needs to hash a lot of
> strings.  I am guessing that perl's internal hashing algorithm is
> implemented at a low level, is optimized, etc. and I would like to
> access it though my perl scripts. Does anyone know how to do this?

I do this by using Perl hash variables :)

It is not clear to me exactly what you want to do.  You want to use
Perl's hashing algorithm from within a Perl script, but without the
rest of the Hash-table machinery?  I suppose you could copy the hashing
algorithm out of the Perl source and use it to create your own XS or
Inline::C code.
perlguts:

The hash algorithm is defined in the "PERL_HASH(hash, key, klen)"
macro:

hash = 0;
while (klen--)
hash = (hash * 33) + *key++;
hash = hash + (hash >> 5);                  /* after 5.6 */

But I don't see what this will get you.  Calling this many times from
within Perl will probably spend more time on function-call overhead than on
hashing.



Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB

Report this thread to moderator Post Follow-up to this message
Old Post
ctcgag@hotmail.com
09-27-04 09:01 PM


Re: accessing perl's internal hashing algorithm
On Mon, 27 Sep 2004 18:00:04 GMT, Ala Qumsieh <notvalid@email.com>
wrote:

>The solution is to preallocate a large enough hash, if you think you can
>  guess how many keys you will need:
>
>	keys %hash = 5000;

Cool! A good example of how precious bits of information can leak in
clpmisc... actually it's all there in the docs, as I have promptly
checked, but, well: would I have done that had I not read this?!?


Michele
--
 {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{po
p^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Report this thread to moderator Post Follow-up to this message
Old Post
Michele Dondi
09-29-04 04:02 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:30 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.