javaMan
javaMan

Reputation: 35

Runtime impact of writing to a file

In this function, I am sorting a list first by the first element of each entry then by fourth element then writing each entry to a file, as shown here:

def sort_and_readToFile(lst):
    address = r"...\prospect\sortedSamples"
    lst.sort(key=lambda x: (x[0],x[3]))
    with open(address, "w") as f:
        for item in lst:
            f.write(str(item[0]) + ',' + str(item[1]) + ',' + str(item[2]) + ',' + str(item[3]) + '\n')
    f.close()

What's the runtime impact of writing to a file in this case? Will most of the runtime be for writing to the file (versus sorting the list)?

Upvotes: 0

Views: 74

Answers (1)

Jayaraj P
Jayaraj P

Reputation: 199

Let's measure the time taken for both and see: -

def sort_and_readToFile(lst):
    address = r"..\prospect\sortedSamples"

    start_time_sort_ms = time.perf_counter() * 1000
    lst.sort(key=lambda x: (x[0], x[3]))
    print("Time taken to sort the list = ", (time.perf_counter() * 1000) - start_time_sort_ms)

    start_time_write_ms = time.perf_counter() * 1000
    with open(address, "w") as f:
        for item in lst:
            f.write(str(item[0]) + ',' + str(item[1]) + ',' + str(item[2]) + ',' + str(item[3]) + '\n')
    print("Time taken to write to file = ", (time.perf_counter() * 1000) - start_time_write_ms)


sort_and_readToFile(['3343', '4223', '2454', '1664', '7666', '3555'])

The output I get is: -

Time taken to sort the list in ms =  0.008400000000008845  
Time taken to write to file in ms =  0.32550000000000523

Now, the time taken will of course depend on the number of elements in the list and the machine you are using. But the important point is that sorting a list is much faster than writing to a file (almost 40 times in this case). This is because sorting is an operation on RAM and writing is an operation on Disk (I use SSD by the way, which is faster than normal HDD, but you get the point).

By the way, since you are opening the file using with, you can avoid f.close().

Upvotes: 1

Related Questions