问题描述:

If my code is calling a function, and one of the function's arguments will vary based on a certain condition, is it more efficient to have the conditional statement as an argument of the function, or to call the function multiple times in the conditional statement.

Example:

(if condition (+ 4 3) (+ 5 3))

(+ (if condition 4 5) 3)

Obiously this is just an example: in the real scenario the numbers would be replaced by long, complex expressions, full of variables. The if might instead be a long cond statement.

Which would be more efficient in terms of speed, space etc?

网友答案:

Don't

What you care about is not performance (in this case the difference will be trivial) but code readability.

Remember,

"... a computer language is not just a way of getting a computer to perform operations, but rather ... it is a novel formal medium for expressing ideas about methodology"

Abelson/Sussman "Structure and Interpretation of Computer Programs".

You are writing code primarily for others (and you yourself next year!) to read. The fact that the computer can execute it is a welcome fringe benefit. (I am exaggerating, of course, but much less than you think).

Okay...

Now that you skipped the harangue (if you claim you did not, close your eyes and tell me which specific language I mention above), let me try to answer your question.

If you profiled your program and found that this place is the bottleneck, you should first make sure that you are using the right algorithm.

E.g., using a linearithmic sort (merge/heap) instead of quadratic (bubble/insertion) sort will make much bigger difference than micro-optimizations like you are contemplating.

Then you should disassemble both versions of your code; the shorter version is (ceteris paribus) likely to be marginally faster.

Finally, you can waste a couple of hours of machine time repeatedly running both versions on the same output on an otherwise idle box to discover that there is no statistically significant difference between the two approaches.

网友答案:

I agree with everything in sds's answer (except using a trick question -_-), but I think it might be nice to add an example. The code you've given doesn't have enough context to be transparent. Why 5? Why 4? Why 3? When should each be used? Should there always be only two options? The code you've got now is sort of like:

(defun compute-cost (fixed-cost transaction-type)
  (+ fixed-cost
     (if (eq transaction-type 'discount) ; hardcoded magic numbers
         3                               ; and conditions are brittle
         4)))

Remember, if you need these magic numbers (3 and 4) here, you might need them elsewhere. If you ever have to change them, you'll have to hope you don't miss any cases. It's not fun. Instead, you might do something like this:

(defun compute-cost (fixed-cost transaction-type)
  (+ fixed-cost
     (variable-cost transaction-type)))

(defun variable-cost (transaction-type)
  (case transaction-type
    ((employee) 2)           ; oh, an extra case we'd forgotten about!
    ((discount) 3)
    (t 4)))

Now there's an extra function call, it's true, but computation of the magic addend is pulled out into its own component, and can be reused by anything that needs it, and can be updated without changing any other code.

相关阅读:
Top