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

  • Home
  • SEARCH
  • 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 6000237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:39:53+00:00 2026-05-23T00:39:53+00:00

According to this question and this answer , lists are implemented as arrays: Perl

  • 0

According to this question and this answer, lists are implemented as arrays:

Perl implements lists with an array
and first/last element offsets. The
array is allocated larger than needed
with the offsets originally pointing
in the middle of the array so there is
room to grow in both directions
(unshifts and pushes/inserts) before a
re-allocation of the underlying array
is necessary. The consequence of this
implementation is that all of perl’s
primitive list operators (insertion,
fetching, determining array size,
push, pop, shift, unshift, etc.)
perform in O(1) time.

So you would expect accessing an element by a numeric offset would be just as fast because they’re arrays in the implementation, which provide very fast constant-time indexing. However, in a footnote in Learning Perl, the author says

Indexing into arrays is not using
Perl’s strengths. If you use the pop,
push, and similar operators that avoid
using indexing, your code will
generally be faster than if you use
many indices, as well as avoiding
“off-by-one” errors, often called
“fencepost” errors. Occasionally, a
beginning Perl programmer (wanting to
see how Perl’s speed compares to C’s)
will take, say, a sorting algorithm
optimized for C (with many array index
operations), rewrite it
straightforwardly in Perl (again, with
many index operations) and wonder why
it’s so slow. The answer is that using
a Stradivarius violin to pound nails
should not be considered a sound
construction technique.

How can this be true when a list is really an array under the hood? I know it’s simply ignorant to try to compare the speed of Perl to C, but wouldn’t indexing a list by offset be just as fast as pop or push or whatever? These seem to contradict each other.

  • 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-23T00:39:54+00:00Added an answer on May 23, 2026 at 12:39 am

    It’s to do with the implementation of Perl as a series of opcodes. push, pop, shift and unshift are all opcodes themselves, so they can index into the array they’re manipulating from C, where the accesses are very fast. If you do this from Perl with indices you’ll make Perl perform extra opcodes to get the index from the scalar, get the slot from the array, then put something into it.

    You can see this by using the -MO=Terse switch to see what Perl is really (in some sense) doing:

    $foo[$i] = 1
    
        BINOP (0x18beae0) sassign
            SVOP (0x18bd850) const  IV (0x18b60b0) 1
            BINOP (0x18beb60) aelem
                UNOP (0x18bedb0) rv2av
                    SVOP (0x18bef30) gv  GV (0x18b60c8) *foo
                UNOP (0x18beba0) null [15]
                    SVOP (0x18bec70) gvsv  GV (0x18b60f8) *i
    
    push @foo, 1
    
        LISTOP (0x18bd7b0) push [2]
            OP (0x18aff70) pushmark
            UNOP (0x18beb20) rv2av [1]
                SVOP (0x18bd8f0) gv  GV (0x18b60c8) *foo
            SVOP (0x18bed10) const  IV (0x18b61b8) 1
    

    You see that Perl has to perform fewer steps, so can be expected to be faster.

    The trick with any interpreted language is to let it do all the work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to this question's answer : According to the standards you must define i
According to the answer to this question, WebLogic uses the JAX-WS RI internally to
According to this question a .Net dictionary resizes its allocated space to prime numbers
According to the answer of this question , the :bd command should not quit
According to the accepted answer to this question GridView row height , if you...
According to the answer to this question MS WF state machine workflows and MS
According to the answers to this question, I cannot embed a file version in
According to this question it seems like you can do this for Methods. What
According to this question I succeeded to create upload image, but now I need
According to this http://perldoc.perl.org/UNIVERSAL.html I shouldn't use UNIVERSAL::isa() and should instead use $obj->isa() or

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.