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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:55:09+00:00 2026-05-25T23:55:09+00:00

I was trying to do some tests on my external sorting algorithms, and I

  • 0

I was trying to do some tests on my external sorting algorithms, and I thought I should generate a huge amount of random numbers and put them into a file.

Here is how I do it:

import tempfile, random

nf = tempfile.NamedTemporaryFile(delete=False)
i = 0
while i < 1000:
    j = 0
    buf = ''
    while j < 1000:
        buf += str(random.randint(0, 1000))
        j += 1
    nf.write(buf)
    i += 1

I thought, I should speed up the generating process by reducing the File IO operations, so I use buf to store as many numbers as possible, then write buf to the file.

Question:

I still got a sense that, the generating and writing process was slow.

Am I getting something wrong?

EDIT:

In C++, we can simply write an int or a float into file by << without converting them into string.

So can we do the same in Python? I mean write an integer into file without converting it into str.

  • 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-25T23:55:10+00:00Added an answer on May 25, 2026 at 11:55 pm

    Operating systems are already optimized for such I/O operations. So, you can directly write the numbers to file and get a very good speed:

    import tempfile, random
    
    with tempfile.NamedTemporaryFile(delete=False) as nf:
        for _ in xrange(1000000):  # xrange() is more efficient than range(), in Python 2
            nf.write(str(random.randint(0, 1000)))
    

    In practice, the numbers will only be written to the disk when the size-optimized file buffer is full. The code in the question and the code above take the same time, on my machine. So, I would advise to use my simpler code and to rely on the operating system’s built-in optimizations.

    If the result fits in memory (which is the case for 1 million numbers), then you can indeed save some I/O operations by creating the final string and then writing it in one go:

    with tempfile.NamedTemporaryFile(delete=False) as nf:
        nf.write(''.join(str(random.randint(0, 1000)) for _ in xrange(1000000)))
    

    This second approach is 30% faster, on my computer (2.6 s instead of 3.8 s), probably thanks to the single write call (instead of a million write() calls–and probably many fewer actual disk writes).

    The “many big writes” approach of your question falls in the middle (3.1 s). It can be improved, though: it is clearer and more Pythonic to write it like this:

    import tempfile, random
    
    with tempfile.NamedTemporaryFile(delete=False) as nf:
        for _ in xrange(1000):
            nf.write(''.join(str(random.randint(0, 1000)) for _ in xrange(1000)))
    

    This solution is equivalent to, but faster than the code in the original question (2.6 s on my machine, instead of 3.8 s).

    In summary, the first, simple approach above might be fast enough for you. If it is not and if the whole file can fit in memory, the second approach is both very fast and simple. Otherwise, your initial idea (fewer writes, bigger blocks) is good, as it is about as fast as the “single write” approach, and still quite simple, when written as above.

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

Sidebar

Related Questions

I'm trying to generate code coverage reports with EMMA using tests of which some
I am trying to run some unit tests in a C# Windows Forms application
I am trying to run PartCover to test the coverage of some tests with
In eclipse 3.4 I'm trying to do some performance tests on a large product,
I've recently been trying to create units tests for some legacy code. I've been
I'm trying some of the ASP.NET MVC tutorials and one of them has the
I am trying to run some tests using OPENmpi processing data in an array
I am trying to run some unit tests for my grails app, via grails
I'm trying to write some Junit tests to test old classes in our app.
I’m thinking about trying some development for the iPhone, is it possible to install

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.