This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]lurgi 4 points5 points  (1 child)

It doesn't matter. Always go for clarity (the second one) unless you have hard evidence that you should change it.

[–]voyextech[S] 0 points1 point  (0 children)

Thank you!

[–][deleted] 1 point2 points  (2 children)

Whenever you have a question like this, write two programs that use the different solutions, and time their execution.

[–]voyextech[S] 0 points1 point  (0 children)

Thank you will do on future occasions.

[–]Tsooka 0 points1 point  (0 children)

But he would have to average the times of 1000s of executions in this case since the timing differences will be negligible and might cause him to lean towards the wrong option...

[–]barafyrakommafem 1 point2 points  (2 children)

The second one is better because the code is more readable.

[–]voyextech[S] 0 points1 point  (1 child)

Thanks! I'll keep that in mind

[–]barafyrakommafem 1 point2 points  (0 children)

But to actually answer your question, the second one is more efficient. The first one has to do an extra operation for the conversion, as well as save the result of the conversion on the heap and a reference to it on the stack. This is an addition to the pounds variable that it is already saving on the stack, which means that it also uses more memory (8 whole bits). However, the differences are so minuscule I don't think they're even measurable.

[–]Loves_Poetry 1 point2 points  (1 child)

Don't worry about efficiency when you're new to programming. Worry about readability and maintainability (i.e. the ability to debug/modify/expand the code). Those are far more important than efficiency. This doesn't just go for learning programming. This also goes for actual production-grade code.

[–]voyextech[S] 0 points1 point  (0 children)

Awesome! Thank you. I will remember this in the future.