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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:47:05+00:00 2026-06-13T23:47:05+00:00

Why is the implementation of startwith slower than slicing? In [1]: x = ‘foobar’

  • 0

Why is the implementation of startwith slower than slicing?

In [1]: x = 'foobar'

In [2]: y = 'foo'

In [3]: %timeit x.startswith(y)
1000000 loops, best of 3: 321 ns per loop

In [4]: %timeit x[:3] == y
10000000 loops, best of 3: 164 ns per loop

Surprisingly, even including calculation for the length, slicing still appears significantly faster:

In [5]: %timeit x[:len(y)] == y
1000000 loops, best of 3: 251 ns per loop

Note: the first part of this behaviour is noted in Python for Data Analysis (Chapter 3), but no explanation for it is offered.

.

If helpful: here is the C code for startswith; and here is the output of dis.dis:

In [6]: import dis

In [7]: dis_it = lambda x: dis.dis(compile(x, '<none>', 'eval'))

In [8]: dis_it('x[:3]==y')
  1           0 LOAD_NAME                0 (x)
              3 LOAD_CONST               0 (3)
              6 SLICE+2             
              7 LOAD_NAME                1 (y)
             10 COMPARE_OP               2 (==)
             13 RETURN_VALUE        

In [9]: dis_it('x.startswith(y)')
  1           0 LOAD_NAME                0 (x)
              3 LOAD_ATTR                1 (startswith)
              6 LOAD_NAME                2 (y)
              9 CALL_FUNCTION            1
             12 RETURN_VALUE 
  • 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-13T23:47:06+00:00Added an answer on June 13, 2026 at 11:47 pm

    Some of the performance difference can be explained by taking into account the time it takes the . operator to do its thing:

    >>> x = 'foobar'
    >>> y = 'foo'
    >>> sw = x.startswith
    >>> %timeit x.startswith(y)
    1000000 loops, best of 3: 316 ns per loop
    >>> %timeit sw(y)
    1000000 loops, best of 3: 267 ns per loop
    >>> %timeit x[:3] == y
    10000000 loops, best of 3: 151 ns per loop
    

    Another portion of the difference can be explained by the fact that startswith is a function, and even no-op function calls take a bit of time:

    >>> def f():
    ...     pass
    ... 
    >>> %timeit f()
    10000000 loops, best of 3: 105 ns per loop
    

    This does not totally explain the difference, since the version using slicing and len calls a function and is still faster (compare to sw(y) above — 267 ns):

    >>> %timeit x[:len(y)] == y
    1000000 loops, best of 3: 213 ns per loop
    

    My only guess here is that maybe Python optimizes lookup time for built-in functions, or that len calls are heavily optimized (which is probably true). It might be possible to test that with a custom len func. Or possibly this is where the differences identified by LastCoder kick in. Note also larsmans‘ results, which indicate that startswith is actually faster for longer strings. The whole line of reasoning above applies only to those cases where the overhead I’m talking about actually matters.

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

Sidebar

Related Questions

The implementation of the addAll method in java.util.Collections simply loops through the source collection
The implementation of a std::allocator (based on MAllocator ) backed by Windows named shared
My implementation of Sieve of Atkin either overlooks primes near the limit or composites
The implementation of Enumerable.AsEnumerable<T>(this IEnumerable<T> source) simply returns source . However Observable.AsObservable<T>(this IObservable<T> source)
My implementation of QTabWidget is not detecting its tabCloseRequested() and currentChanged() signals. TileSheetManager::TileSheetManager(QWidget *parent)
In the tableView:cellForRowAtIndexPath: implementation of my UITableViewController subclass I'm using this code snippet: AlarmPicker
Is there an efficient implementation of matlab's deconv in python? # Convolve z=conv(x, y)
Looking at the Ehcahce implementation of net.sf.cache.JS107, I am trying to achieve the following
The GetPattern() method implementation of WPF UI Automation system is implemented taking the enum
I have a c++ implementation file (my.cpp) that indirectly includes a header file (b.h)

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.