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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:26:49+00:00 2026-05-27T23:26:49+00:00

Warning, this is a bit recursive ;) I answered this question: Python:How can i

  • 0

Warning, this is a bit recursive 😉

I answered this question: Python:How can i get all the elements in a list before the longest element?

And after I submitted there where another answer that should be faster (the author thought, and so did I). I tried to time the different solutions but the solution that should be slower was actually faster. This made me think there is something wrong with my code. Or is it?

import string
import random
import time

def solution1(lst):
  return lst[:lst.index(max(lst, key=len))]

def solution2(lst):
  idx, maxLenStr = max(enumerate(lst), key=lambda x:len(x[1]))
  return lst[:idx]

# Create a 100000 elements long list that contains
# random data and random element length
lst = []
for i in range(100000):
  s = "".join([random.choice(string.letters+string.digits) for x in range(1, random.randint(1,50))])
  lst.append(s)

# Time the first solution
start = time.time()
solution1(lst)
print 'Time for solution1', (time.time() - start)

# Time the second solution
start = time.time()
solution2(lst)
print 'Time for solution2', (time.time() - start)

Update

Before anyone mentions why I put this is as a new question. The question is more about me learning how to measure execution time…

  • 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-27T23:26:50+00:00Added an answer on May 27, 2026 at 11:26 pm

    The lambda is costing dearer in the second solution.

    I profiled both the codes and by the profile data, it looks, the first solution is faster

    As the wiki would say function call is costly and in the second solution, the lambda and the len function calls are making it run slower

    Please note, I have trimmed down the list to a length of 1000 elements

    >>> cProfile.run('solution1(lst)')
             5 function calls in 0.000 CPU seconds
    
       Ordered by: standard name
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
            1    0.000    0.000    0.000    0.000 <pyshell#305>:1(solution1)
            1    0.000    0.000    0.000    0.000 <string>:1(<module>)
            1    0.000    0.000    0.000    0.000 {max}
            1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
            1    0.000    0.000    0.000    0.000 {method 'index' of 'list' objects}
    
    
    >>> cProfile.run('solution2(lst)')
             2004 function calls in 0.012 CPU seconds
    
       Ordered by: standard name
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
            1    0.000    0.000    0.012    0.012 <pyshell#306>:1(solution2)
         1000    0.006    0.000    0.009    0.000 <pyshell#306>:2(<lambda>)
            1    0.000    0.000    0.012    0.012 <string>:1(<module>)
         1000    0.003    0.000    0.003    0.000 {len}
            1    0.003    0.003    0.012    0.012 {max}
            1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run this bit of code through GCC I get this warning on
Warning: This question has been heavily edited. I tried my best to guess the
Is this warning anything to worry about? I've read that it can cause erratic
I get this warning from GCC: warning: cannot pass objects of non-POD type 'class
I get this: Warning: session_start(): Cannot send session cookie - headers already sent by
I get this warning in my error logs and wanted to know how to
This question has been asked in various forms on here before, but I cannot
All this originated from me poking at a compiler warning message (C4267) when attempting
EDIT Public health warning - this question includes a false assumption about undefined behaviour.
Warning: This is inherited legacy code that was initially put together in the early

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.