0

What I mean is which would be faster for a computer to execute.

The following code is in python3.

for in range(10):
    print('billy bob')

OR

print('billy bob')
print('billy bob')
print('billy bob')
print('billy bob')
print('billy bob')
print('billy bob')
print('billy bob')
print('billy bob')
print('billy bob')
print('billy bob')

if you don't understand what I mean, just say in comments, ill try to answer.

epiconan
  • 11
  • 1
  • In this case it would make no difference at all, as the I/O is several orders of magnitude slower than the loop mechanism, but there is a compiler optimization called 'loop unrolling', so draw your own conclusion. The question about looping *vs* recursion isn't the same at all. – user207421 May 21 '20 at 00:00
  • thanks, i will study those things. also, how isn’t the other question the same? also, how did you find this question so quickly – epiconan May 21 '20 at 00:12
  • What makes you think the other question *is* the same? It isn't the same, because loop unrolling isn't the same as recursion. Surely this is obvious? I found it because it was at the top of the list when I refreshed the main page. – user207421 May 21 '20 at 00:18
  • 1
    It's not the same because your question has nothing to do with recursion. There is no recursion in your first code snippet, which means that recursion does not apply. Also, you're engaging in unnecessary premature optimization. Don't worry about which is faster or more inefficient until you've run your code through a profiler and that profiler identifies this specific code as a bottleneck, at which point you can start worrying about how to optimize it. IOW, use the loop, which is cleaner and easier to read and requires much less typing. – Ken White May 21 '20 at 00:25
  • Oh. Sorry, I thought that recursion was the same meaning as repeating. i will remove that part now. – epiconan May 21 '20 at 00:27
  • To Ken White: I obviously know to use a for loop when I need to repeat something. (so I would use the loop) I was just curious about the question after my friend said that copy-pasting has no difference with a loop in terms of the time for the computer to run. -from someone who isnt very good at programming, so please bear with me. – epiconan May 21 '20 at 00:41
  • Thank you everyone who spent their time answering this question. I am also sorry for thinking recursion and repeating are the same thing. (after spending some time googling, i realized they are completely different things. i just assumed they were the same thing because recursion sounds like it means repeating.) I won't spend my time on "premature optimization". (i also googled that and it fits what i am doing with this question.) i didnt expect answers so fast (thanks) – epiconan May 21 '20 at 00:50
  • Your friend doesn't know what he's talking about, unless he is making the same point that I did in my first comment. – user207421 May 21 '20 at 01:27

0 Answers0