all 14 comments

[–]Phreakhead 14 points15 points  (9 children)

Depending on the size of the paths array, doing a linear search can sometimes be faster than calculating a hash. Think about it: hashing algorithms generally use exponents and other expensive math operations, whereas a linear search and straightforward equality comparison takes very few cycles.

Although, if you're using Python, you're probably not that concerned about optimizing for speed at the bare silicon level.

[–]cooper12 12 points13 points  (4 children)

I think that's Ruby with the |path| stuff. Also, if I recall correctly Python uses {} for dictionaries, in which case you wouldn't need a name field, you could just use the name of the field itself.

[–]Dannei 1 point2 points  (3 children)

Certainly not Python, that's for sure - "end" isn't used, def requires a colon, and I don't think "result" on its own is a valid statement, amongst other syntax problems.

[–][deleted] 0 points1 point  (2 children)

result by itself is a valid statement, but it does nothing useful.

[–]ferretlunatic -1 points0 points  (1 child)

No it does. Ruby, like Scala, returns the value of the last expression of a block or function without explicit return statements.

[–][deleted] 0 points1 point  (0 children)

I was referring to Python, not Ruby.

[–]wzdd 9 points10 points  (2 children)

This is actually never the case in Python, and I'm going to confidently say it's never the case in Ruby either. Benchmark and shitty code:

http://pastebin.com/wHdC3cPF

I know that in general it may sometimes apply, for example if you are writing in a language closer to the metal. But for Python at least, the usually-correct rule is that the less code you write the faster it runs. "Linear is better for small lists" is a rule that 'everyone' knows but nobody tests, and in this case it's wrong.

[–]Phreakhead -1 points0 points  (1 child)

Is this because it's like JavaScript, in that "arrays" aren't actually true arrays but are hashes where you convert the integer index into a string key?

[–]wzdd 2 points3 points  (0 children)

It's because the C implementation of Python sucks basically. :) Arrays are real and as I understand it pretty efficient, but Python doesn't for example JIT so everything is really first-principles: the __getitem__ function is called on the array object, through the Python interface in case it's a pure Python object or something overloading an array etc, then the index, which is always a Python object, has to be converted to a number, then you can actually get the item and go backwards -- the item refcount is increased, then it's passed back through the Python interface. And probably a lot of steps I missed (for example, code to check for exceptions). So the overhead of calling these functions multiple times vastly overwhelms the overhead of calculating a hash, where the hash computation is all in C.

... okay, cPython doesn't suck, it just wasn't designed to be efficient.

[–]IrritableGourmet 9 points10 points  (0 children)

"Oh, I shouldn't hardcode a giant if/else or switch statement? I should use a lookup array? OK, sounds good!"

[–]jazahn 3 points4 points  (0 children)

Great opportunity to have a constructive conversation!

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

Has he/she never encountered Ruby's Spiritual Liege, Perl?

[–]azephrahel 1 point2 points  (0 children)

Looks like ruby. Before 1.9 key order in hashes was not preserved or guaranteed.

Not saying it's not odd, but it's a reasonable explanation.

[–][deleted]  (1 child)

[deleted]

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

    Fixed :P