python write text to file slower than printing text to terminal with python? -


I am writing a program that takes a string and calculates all possible repeating permutations from this string. I will show some pieces of my code, I would be grateful if someone could talk to me how to improve the speed at the time of sending the data to the data.

Scenario 1

For output of stdout, itertools import itertools.product to 531,441 lines of words (3mb)

  To write (abcdefghi, repeat = 6): print (word)   

scenario 2

Then I send output to a file instead of stdout Tried, and it took roughly 5 minutes to take a rough time. Import itertools into word_counter = itertools.product for word 0 (abcdefghi, repeat = 6): word_counter = word_counter + 1 if word_counter == 1: open ('myFile', 'Mau' ) Write (word) someone else. Open ('myFile', 'A'). Keep track of the number of repeated permutations as writing (word)

the wordquint function is looping. When WordQoint creates a file in the program 1 and then attach the file to the data, when the word-corner is greater than 1.

I use a program on the Web to do this and I found that at the same time the program was printed in the terminal and the same web project had to output these combinations to a file. Took about 3 seconds to take, while my program took 5 minutes to output data to the file!

I also tried to run my program and redirect the output to a file in one terminal, and it took the same time (3 seconds)!

  'myprog' & gt; 'Output file'  

You are reopening the file for every write, Do not try to do this:

  import itertools to output itertools.product = open for word ('myFile', 'W') (abcdefghi, repeat = 6): output.write ( Word + '\ n')   

[edit with explanation] Are you working with 530,000 words, even a little slow for each word Make a very slow schedule for the whole program If the call.

On my way, you do a piece of setup work (open the file) and put it in memory, then pass through 500,000 words and save them, then a piece of clean work (file Stop it). This is the reason that the file is saved in one variable - so you can set it once, and use it again.

Your way, you do not work almost any setup, then you add one to the counter 500,000 times, check the value of the counter 500,000 times, branch like this or 500,000 times, open the file. And always force Windows (or Linux) to check your permissions, keep it in memory 500,000 times, write it 500,000 times, stop using the file you opened (because you did not save it), so It should be in 'garbage' Breakfast and it is Trid - 500,000 times, and finish later.

The amount of work is small every time, but when you do them several times, it says.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -