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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:46:59+00:00 2026-05-18T03:46:59+00:00

Is this the fastest (execution time) way to find the longest element in a

  • 0

Is this the fastest (execution time) way to find the longest element in a list?

#!/usr/bin/env perl
use warnings;
use 5.012;
use List::Util qw(reduce);
use List::Util::XS;

my @array = qw( one two three four five six seven eight nine ten eleven );

my $l = reduce{ length($a) > length($b) ? $a : $b } @array;

say $l;
  • 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-18T03:47:00+00:00Added an answer on May 18, 2026 at 3:47 am

    When only trying to find one element of a list, there is no need to construct an N sized data structure as many answers here have done. The fastest O(N) way to do this is to walk the array, keeping track of the largest element. That way you have O(N) accesses of the list, and O(1) memory usage.

    sub longest {
        my $max = -1;
        my $max_i = 0;
        for (0 .. $#_) {              # for each index
            my $len = length $_[$_];  # only get length once per item
            if ($len > $max) {        # save index and update max if larger
                $max = $len;
                $max_i = $_;
            }
        }
        $_[$max_i]   # return the largest item
    }
    

    If you are going to be running the above code many times, I would suggest inlining the body of the subroutine.

    EDIT:

    drewk’s benchmark revealed that the array index in the above code is a bit of a bottleneck. Experimenting a little more, I have finally found a method that is faster than the reduce solution:

    sub fastest {
        my $max = -1;
        my $max_ref;
        for (@_) {
            if (length > $max) {  # no temp variable, length() twice is faster
                $max = length;
                $max_ref = \$_;   # avoid any copying
            }
        }
        $$max_ref
    }
    

    which results in the following benchmark:

               Rate longest   drewk  reduce fastest
    longest 44245/s      --    -21%    -30%    -47%
    drewk   55854/s     26%      --    -11%    -33%
    reduce  63014/s     42%     13%      --    -25%
    fastest 83638/s     89%     50%     33%      --
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using PHP, what's the fastest way to convert a string like this: 123 to
Whats the fastest possible way to do this? I think the code below works,
I know bubble sort is probably not the fastest way to do this but
This most be the second most simple rollover effect, still I don't find any
What is the fastest way to sum up an array in JavaScript? A quick
What is the fastest way to count lines and words in a text file
What is the fastest way to check if a string matches a certain pattern?
I have found this example on StackOverflow: var people = new List<Person> { new
I want to use a temp directory that will be unique to this build.
Time of execution: foo(1) >>> foo(2) >> foo(3) roughly: 1427349 >>> 14757 >> 1362

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.