2

So I'm trying to decide which of these is more efficient, or if there is a difference between them. The program I'm writing iterates through a for loop (python) and does some stuff, and then, depending on a flag, will write to a file.

Example A:

for element in list:
    Do stuff
    if(write_to_file):
       write to file

Vs example B:

for element in list:
    Do stuff
if(write_to_file):
    for element in list:
        write to file

In the case of A it has to check every time if it's true, but in the case of B if it is true it has to then re-do the for loop. My thought is that they're equal, but I'd like opinions of more experienced programmers

6
  • 11
    You are writing to a file, ergo, you are worrying over something that'll fall into nothingness compared to the time the I/O takes. :-) Commented May 22, 2014 at 13:36
  • Use timeit and find out: docs.python.org/2/library/timeit.html Commented May 22, 2014 at 13:37
  • 4
    There's no substitute for testing it yourself. Commented May 22, 2014 at 13:37
  • @IanAuld, Henry Keiter: Thanks, I didn't know that function existed. Will do Commented May 22, 2014 at 13:42
  • check out ipython if you haven't yet... Commented May 22, 2014 at 13:47

2 Answers 2

2

Based on the Big O complexity analysis of your example(s):

for element in list:
    Do stuff
if(write_to_file):
    for element in list:
        write to file

The above is less efficient because you are iterating over list twice.

i.e: O(n) x 2

But please test yourself, use the timeit module and most importantly of all you are worrying prematurely about optimization. get it working first!

Read also: When to optimize

Sign up to request clarification or add additional context in comments.

6 Comments

If write_to_file is false, this is more efficient, because there's only a single loop iteration and the condition is checked only once, rather than each time through the loop.
Except for when it's True. Yes it all depends. The OP has not provided any specifics so this is the best answer I can provide based on what I see :)
@JamesMills, it's based on user input so completely random. Based on your input and the input in the comments on the OP I tried using timeit on it: Example A: Write_to_file = false: 18.05 ms write_to_file = true: 18.60 ms Ex B: Write to file = true:19.4 ms Writeto file = false: 18.27 ms Not much difference. And don't worry, it was working before I asked!
@musher: user input is anything but completely random!
@BenjaminHodgson I'm actually curious about this - what do you mean?
|
1

Example C:

if(write_to_file):
    for element in list:
        Do stuff
        write to file
else:
    for element in list:
        Do stuff

As others have said, you will probably not be able notice the difference between any of these approaches.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.