You call this optimized?

9

1

The task is simple, post 2 code snippets that perform the same task, where one seems very optimized at first glance, but actually is significantly slower than the other.

Only one rule:

  • Both snippets must finish with the exact same output (for the same given input, if any).

vrwim

Posted 2015-07-06T04:43:12.213

Reputation: 2 223

Question was closed 2015-07-06T07:01:32.333

1Without choosing a specific task, I think this is way too broad to be interesting. You should also specify if both snippets have to use the same language. – Dennis – 2015-07-06T04:49:27.323

@Dennis yeah it might be too broad, I just wanted to see some creative approaches. Also there is no restriction on languages, so that one can get more creative freedom. – vrwim – 2015-07-06T04:52:04.973

9Creative freedom doesn't lead to creative answers. It requires a lot more creativity to solve a difficult task than an easy one. – Dennis – 2015-07-06T04:57:13.330

Answers

11

Python 3

Seems fast...

 print(0 < 10**100 - 1 < 10**100)

Seems slow...

 print(10**100 - 1 in range(0, 10**100))

But the two run in very similar times! range has an efficient implementation of in

Caridorc

Posted 2015-07-06T04:43:12.213

Reputation: 2 254

Question closed aside, isn't this doing the opposite of what the question's asking? You want something that looks fast but is actually slow, not something that looks slow but is actually fast... – Sp3000 – 2015-07-06T11:41:34.013

4@Sp3000 right but it was so broad that I decided to post an even broader answer. – Caridorc – 2015-07-06T11:44:54.303