Question


So the problem is - which case to use? I got list of urls and got some magic function get_text_from_url - and I have to write this into the file with given filename - simply as can be, but which case of this two is better than another and why?
The priority in this case is time and memory - not readability (sorry, Python).
And, of course, any other versions are welcome.
First:

def create_text(filename):        
    delete_current_text(filename)
    urllist = get_urls()
    for url in urllist:
        with open(filename, "a") as file:
             print >>file, get_text_from_url(url)


Second:

def create_text(filename):        
    urllist = get_urls()
    with open(filename, "w") as file:
         for url in urllist:
             print >>file, get_text_from_url(url)
Was it helpful?

Solution

Your second example would be better, since open/close operation on files has significant overhead, and the latter only did it once.

However, it should be even better if you store the result (if get_text_from_url won't return a too big content), like this:

def create_text(filename):
    urllist = get_urls()
    result = map(get_text_from_url, urllist)
    with open(filename, "w") as file:
        file.write("\n".join(result))

So there's only one write, and only blocks the file for a short period of time (assuming that get_text_from_url will do web request, which costs much longer time than local file writes).

OTHER TIPS

The I/O will be faster if you open the file, do the writes, then close it when you're done. In reality, you're choosing between "open once, write n", and "open n, write n" (not "open n, write once" as you stated). With the latter option, you're only doing one write per open, but you're still doing n writes total.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top