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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:10:12+00:00 2026-06-03T06:10:12+00:00

I recently wrote a quick and dirty BFS implementation, to find diamonds in a

  • 0

I recently wrote a quick and dirty BFS implementation, to find diamonds in a directed graph.
The BFS loop looked like this:

while toVisit:
    y = toVisit.pop()
    if y in visited: return "Found diamond"
    visited.add(y)
    toVisit.extend(G[y])

(G is the graph – a dictionary from node names to the lists of their neighbors)

Then comes the interesting part:
I thought that list.pop() is probably too slow, so I ran a profiler to compare the speed of this implementation with deque.pop – and got a bit of an improvement. Then I compared it with y = toVisit[0]; toVisit = toVisit[1:], and to my surprise, the last implementation is actually the fastest one.

Does this make any sense?
Is there any performance reason to ever use list.pop() instead of the apparently much faster two-liner?

  • 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-03T06:10:13+00:00Added an answer on June 3, 2026 at 6:10 am

    You have measured wrong. With cPython 2.7 on x64, I get the following results:

    $ python -m timeit 'l = list(range(10000))' 'while l: l = l[1:]'
    10 loops, best of 3: 365 msec per loop
    $ python -m timeit 'l = list(range(10000))' 'while l: l.pop()'
    1000 loops, best of 3: 1.82 msec per loop
    $ python -m timeit 'import collections' \
             'l = collections.deque(list(range(10000)))' 'while l: l.pop()'
    1000 loops, best of 3: 1.67 msec per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently wrote a quick-and-dirty proof-of-concept proxy server in C# as part of an
I recently found out about C# extension methods and wrote this one: /// <summary>
Just a quick one - I wrote a php script recently that dynamically creates
Recently I wrote this query with Linq (Method Syntax), notes: g.CmsContents is a Navigatiola
I recently wrote a quick VB.NET app that injects a DLL into a running
I recently had to take a quick look at Adobe InDesign server. In this
I recently wrote another post on this same topic in which I was not
Hey, I recently wrote this fairly simple game of life in JavaScript. Within this
Recently i had some performance problems in a SOAP webservice I wrote a while
I recently wrote an iPad app that I would like to make a universal

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.