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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:53:40+00:00 2026-06-07T04:53:40+00:00

I am getting some very surprising results that seem to indicate that it’s more

  • 0

I am getting some very surprising results that seem to indicate that it’s more efficient to wrap an iterator in list and get it’s length compared to walking it with a lambda. How is this possible? Intuition would suggest that allocating all these lists would be slower.

And yes – I am aware that you can’t always do this as iterators can be infinite. 🙂

from itertools import groupby
from timeit import Timer

data = "abbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccac" 

def rle_walk(gen):
    ilen = lambda gen : sum(1 for x in gen)
    return [(ch, ilen(ich)) for ch,ich in groupby(data)]

def rle_list(data):
    return [(k, len(list(g))) for k,g in groupby(data)]

# randomy data
t = Timer('rle_walk("abbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccac")', "from __main__ import rle_walk; gc.enable()")
print t.timeit(1000)

t = Timer('rle_list("abbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccac")', "from __main__ import rle_list; gc.enable()")
print t.timeit(1000)

# chunky blocks
t = Timer('rle_walk("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccc")', "from __main__ import rle_walk; gc.enable()")
print t.timeit(1000)

t = Timer('rle_list("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccc")', "from __main__ import rle_list; gc.enable()")
print t.timeit(1000)

1.42423391342
0.145968914032
1.41816806793
0.0165541172028
  • 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-06-07T04:53:42+00:00Added an answer on June 7, 2026 at 4:53 am

    Unfortunately your rle_walk has a bug; it takes parameter gen but should take parameter data, so it’s operating on the wrong input. Also, it’s unfair to make rle_walk use a lambda where rle_list works inline. Rewriting like so:

    def rle_walk(data):
        return [(k, sum(1 for _ in g)) for k, g in groupby(data)]
    
    def rle_list(data):
        return [(k, len(list(g))) for k, g in groupby(data)]
    

    and testing:

    data_block = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccc"
    data_random = "abbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccacabbbccac"
    print [[Timer('r("{data}")'.format(data=data),
                  "from __main__ import {r} as r; gc.enable()".format(r=r)).timeit(1000)
            for r in ['rle_walk', 'rle_list']]
            for data in (data_block, data_random)]
    

    gives

    [[0.02709507942199707, 0.022060155868530273],
     [0.12022995948791504, 0.16360306739807129]]
    

    so we see that walk is slightly slower than list on the blocky data, but slightly faster on the random data. I’d guess the reason is that generators (in Python) impose an overhead compared to the list constructor; and the memory overhead of a 30-item list is too small to impose any significant penalty.

    Disassembling the functions provides a little insight:

    >>> dis.dis(lambda g: (1 for _ in g))
      1           0 LOAD_CONST               0 (<code object <genexpr> at 0x2b9202a6fe40, file "<stdin>", line 1>)
                  3 MAKE_FUNCTION            0
                  6 LOAD_FAST                0 (g)
                  9 GET_ITER            
                 10 CALL_FUNCTION            1
                 13 RETURN_VALUE        
    >>> dis.dis((lambda g: (1 for _ in g)).func_code.co_consts[0])
      1           0 SETUP_LOOP              18 (to 21)
                  3 LOAD_FAST                0 (.0)
            >>    6 FOR_ITER                11 (to 20)
                  9 STORE_FAST               1 (_)
                 12 LOAD_CONST               0 (1)
                 15 YIELD_VALUE         
                 16 POP_TOP             
                 17 JUMP_ABSOLUTE            6
            >>   20 POP_BLOCK           
            >>   21 LOAD_CONST               1 (None)
                 24 RETURN_VALUE        
    >>> dis.dis(lambda g: len(list(g)))
      1           0 LOAD_GLOBAL              0 (len)
                  3 LOAD_GLOBAL              1 (list)
                  6 LOAD_FAST                0 (g)
                  9 CALL_FUNCTION            1
                 12 CALL_FUNCTION            1
                 15 RETURN_VALUE        
    

    The much larger code volume for the generator form is going to have some effect; while the list form has an O(log n) factor for constructing the throwaway list it’s going to be dominated by the k*O(n) factors in looping the various iterators. One thing to take away from this is that memory allocation is fast, at least for small (sub-page) allocations in a single-threaded environment (which CPython is by necessity of the GIL).

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

Sidebar

Related Questions

I am getting some very interesting results when testing an app developed with MonoDroid
I recently have been getting more into C++. I have done some (very minimal)
I'm getting some very strange behaviour using the following switch statement: string recognise_mti(int mti_code)
I'm getting some very wierd errors. The compiler seems to want to call the
I am looking for some advice in regards to getting a very quick display
I'm getting some very slow performance from an Umbraco site hosted on my Azure
I'm getting some very jerky scrolling while using the code below to create a
I'm working on a VB6 app that will do some very simple communication with
I'm getting some very strange date formatting issues in my Silverlight application. My local
I'm getting some very strange behaviour with tinyMCE in an ASP.NET MVC 2 beta

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.