Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 855367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:03:48+00:00 2026-05-15T08:03:48+00:00

Is it possible to use a Perl hash in a manner that has O(log(n))

  • 0

Is it possible to use a Perl hash in a manner that has O(log(n)) lookup and insertion?

By default, I assume the lookup is O(n) since it’s represented by an unsorted list.

I know I could create a data structure to satisfy this (ie, a tree, etc) however, it would be nicer if it was built in and could be used as a normal hash (ie, with %)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:03:49+00:00Added an answer on May 15, 2026 at 8:03 am

    Associative arrays in Perl 5 are implemented with hash tables which have amortized O(1) (i.e. constant time) insert and lookup. That is why we tend to call them hashes not associative arrays.

    It is difficult to find documentation that states that Perl 5 uses a hash table for implementation of associative arrays (besides the fact that we call associative arrays “hashes”), but there is at least this in perldoc perlfaq4

    What happens if I add or remove keys from a hash while iterating over it?
       (contributed by brian d foy)
    
       The easy answer is "Don't do that!"
    
       If you iterate through the hash with each(), you can delete the key
       most recently returned without worrying about it.  If you delete or add
       other keys, the iterator may skip or double up on them since perl may
       rearrange the hash table.  See the entry for "each()" in perlfunc.
    

    An even better quote from perldoc perldata:

       If you evaluate a hash in scalar context, it returns false if the hash
       is empty.  If there are any key/value pairs, it returns true; more
       precisely, the value returned is a string consisting of the number of
       used buckets and the number of allocated buckets, separated by a slash.
       This is pretty much useful only to find out whether Perl's internal
       hashing algorithm is performing poorly on your data set.  For example,
       you stick 10,000 things in a hash, but evaluating %HASH in scalar
       context reveals "1/16", which means only one out of sixteen buckets has
       been touched, and presumably contains all 10,000 of your items.  This
       isn't supposed to happen.  If a tied hash is evaluated in scalar
       context, a fatal error will result, since this bucket usage information
       is currently not available for tied hashes.
    

    Of course, O(1) is only theoretical performance. In the real world we do not have perfect hashing functions, so hashes do slow down as they get larger, and there are some degenerate cases that turn a hash into O(n), but Perl does its best to prevent this from happening. Here is a benchmark of Perl hashes with 10, 100, 1,000, 10,000, 100,000 keys:

    Perl version 5.012000
                   Rate 10^5 keys 10^4 keys 10^3 keys 10^2 keys 10^1 keys
    10^5 keys 5688029/s        --       -1%       -4%       -7%      -12%
    10^4 keys 5748771/s        1%        --       -3%       -6%      -11%
    10^3 keys 5899429/s        4%        3%        --       -4%       -9%
    10^2 keys 6116692/s        8%        6%        4%        --       -6%
    10^1 keys 6487133/s       14%       13%       10%        6%        --
    

    Here is the benchmark code:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use Benchmark;
    
    print "Perl version $]\n";
    
    my %subs;
    for my $n (1 .. 5) {
        my $m = 10 ** $n;
        keys(my %h) = $m; #preallocated the hash so it doesn't have to keep growing
        my $k = "a";
        %h = ( map { $k++ => 1 } 1 .. $m );
        $subs{"10^$n keys"} = sub {
            return @h{"a", $k};
        }
    };
    
    Benchmark::cmpthese -1, \%subs;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to use a single line foreach loop in Perl? $hash{$thing}++ foreach
Is it possible to use JMF in Android? JMF has good functionality? I'm basically
Is it possible to use perl's move function from File::Copy module to use a
Is possible to use different version of Perl without using the SET or manipulating
I am wondering if it is possible to use a single perl cgi script
Seem like my daily road block. Is this possible? String in qw? #!/usr/bin/perl use
Possible Duplicate: How do you create a Perl module? I have the script that
Possible Duplicate: How can I use Perl to grab text from a web page
Is it possible use mod_rewrite to resolve addresses hosted on another server? Say I
Is it possible use a MySQL query to perform this kind of check? If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.