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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:07:47+00:00 2026-05-18T21:07:47+00:00

I have a program that runs in Python 2 and Python 3, but there

  • 0

I have a program that runs in Python 2 and Python 3, but there is a drastic difference in speed. I understand a number of internal changes were made in the switch, but the difference in io.BufferedReader are really high. In both versions, I use io.BufferedReader because the main program loop only needs data one byte at a time. Here is an excerpt from the cProfile output for the script (see cumtime, not tottime):

Python 2:
 ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 36984   0.188    0.000    0.545    0.000   io.py:929(read)

Python 3:
 36996    0.063   0.000    0.063    0.000   {method 'read' of '_io.BufferedReader' objects}

When I print the object, both return something like io.BufferedReader so I am certain they are both using BufferedReader.

Here is the code in question. See line 28. The caller is responsible for setting up bufstream. I used bufstream = io.open('testfile', 'rb')

Why is there such a drastic difference in speed of BufferedReader for reading single bytes in the files, and how can I “fix” the issue for Python 2.x? I am running Python 2.6 and Python 3.1.

  • 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-18T21:07:47+00:00Added an answer on May 18, 2026 at 9:07 pm

    To give you a fuller answer, one would need to see your code (or, better, an executable precis of your code).

    However a partial answer can be gleaned from your profile output: io.py suggests that “Python 2” (for avoidance of doubt, give the actual version numbers) is implementing BufferedReader in Python, whereas _io.BufferedReader suggests that “Python3” is implementing it in C.

    Late-breaking news: Python 2.6’s io.py is over 64Kb and includes the following comment up the front :

    # This is a prototype; hopefully eventually some of this will be
    # reimplemented in C.
    

    Python 2.7’s io.py is about 4Kb and appears to be a thin wrapper of an _io module.

    If you want real assistance with a workaround for 2.6, show your code.

    Probable workaround for Python 2.6

    Instead of:

    test = io.open('test.bmp', 'rb')

    do this:

    test = open('test.bmp', 'rb')
    

    Some rough timing figures, including the missing link (Python 2.7):

    Windows 7 Pro, 32-bit, approx 5 Mb file, guts of code is:

    while 1:
        c = f.read(1)
        if not c: break
    
    2.6: io.open 20.4s, open 5.1s
    2.7: io.open  3.3s, open 4.8s # io.open is better
    3.1: io.open  3.6s, open 3.6s # effectively same code is used
    

    So a better story seems to be this: In general, don’t faff about with io.open unless you have good reason to e.g. you want 2.7 to go faster.

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

Sidebar

Related Questions

I have a Python program that runs a series of experiments, with no data
I have a program that runs osql.exe from microsoft sql server tools directory and
I have a cross platform program that runs on Windows, Linux and Macintosh. My
I have a Visual Studio 2005 C++ program that runs differently in Release mode
I have a PHP script that runs as a CGI program and the HTTP
I have a bash script that runs a simulation program written in Fortran 90,
I have a program that uses the mt19937 random number generator from boost::random. I
So I have a python program that ends up leaving a .dat file from
I have a program that spits out both standard error and standard out, and
I have a program that creates a Windows user account using the NetUserAdd() API

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.