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 400011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:54:09+00:00 2026-05-12T16:54:09+00:00

Several times I’ve read that unpack() is faster than substr() , especially as the

  • 0

Several times I’ve read that unpack() is faster than substr(), especially as the number of substrings increases. However, this benchmark suggests otherwise. Is my benchmark flawed, or is the alleged performance advantage of unpack() a holdover from older versions of Perl?

use strict;
use warnings;
use Benchmark;

my ($data, $format_string, $n_substrings);

my %methods = (
    unpack => sub { return unpack $format_string, $data },
    substr => sub { return map {substr $data, $_, 1} 0 .. $n_substrings - 1 },
);

for my $exp (1 .. 5){
    $n_substrings = 10 ** $exp;
    print $n_substrings, "\n";
    $format_string = 'a1' x $n_substrings;
    $data          =   9  x $n_substrings;
    Benchmark::cmpthese -2, \%methods;
}

Output (on Windows):

10
           Rate unpack substr
unpack 131588/s     --   -52%
substr 276802/s   110%     --
100
          Rate unpack substr
unpack 13660/s     --   -57%
substr 31636/s   132%     --
1000
         Rate unpack substr
unpack 1027/s     --   -68%
substr 3166/s   208%     --
10000
         Rate unpack substr
unpack 84.4/s     --   -74%
substr  322/s   281%     --
100000
         Rate unpack substr
unpack 5.46/s     --   -82%
substr 30.1/s   452%     --

As pointed out in some answers, unpack() does poorly on Windows. Here’s the output on a solaris machine — not nearly so decisive, but substr() still wins the foot race:

10
           Rate unpack substr
unpack 202274/s     --    -4%
substr 210818/s     4%     --
100
          Rate unpack substr
unpack 22015/s     --    -9%
substr 24322/s    10%     --
1000
         Rate unpack substr
unpack 2259/s     --    -9%
substr 2481/s    10%     --
10000
        Rate unpack substr
unpack 225/s     --    -9%
substr 247/s     9%     --
100000
         Rate unpack substr
unpack 22.0/s     --   -10%
substr 24.4/s    11%     --
  • 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-12T16:54:09+00:00Added an answer on May 12, 2026 at 4:54 pm

    Since asking this question, I have benchmarked substr against unpack several more times, under various conditions. Here are a few things I’ve learned:

    • Do not set up the benchmark in a way
      that calls Perl functions in void
      context (as I did in my original question; see the
      helpful response from dlowe).
      Some Perl functions have
      optimizations when they are called in
      void context (and these optimizations
      appear to vary by OS), potentially
      skewing your benchmarking results.

    • If your use of substr involves
      looping (for example, iterating over
      a list of column locations), unpack
      is always faster. However, the
      apparent slowness of substr in this
      situation is due to the overhead of
      the loop, not substr itself.

    • If just a few fields are required,
      substr is generally faster or as
      fast as unpack.

    • If more than a few fields are
      required, head-to-head comparisons
      between unpack and an equivalent
      number of substr calls do not vary
      much as the number of fields
      increases: both approaches become
      slower at the same rate.

    • Results can vary by operating system.
      On my Windows XP machine, unpack
      had a slight edge whenever more than
      a few fields were needed. On our
      Solaris machines at my workplace,
      substr was always faster, even into
      hundreds of fields.

    Bottom line: the performance of unpack vs. substr is not a very big issue, regardless of the number of fields. Use whichever approach results in the clearest code. If you find yourself using substr in a looping construct, however, switching to unpack will result in a noteworthy speed boost.

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

Sidebar

Related Questions

Several times I heard that PHP hosting is much cheaper than ASP.NET one. I
I know this question has been asked several times, however, after digging through question
I've heard several times that several problems arise in hibernate (especially when using lazy
I have heard this several times, that one should always update before committing to
I read spring docs on this subject several times, but some things are still
Several times I've seen ReSharper generate code that looks like this: delegate void myHandler(int
I have read several times that after you delete a row in an InnoDB
I've read this several times now: some developers aren't advocates of interleaving JSF/Facelets tags
I've googled this question several times, but I can't find something that works. I
I know that this has been discussed several times in many other topics, blogs,

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.