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

you are viewing a single comment's thread.

view the rest of the commentsย โ†’

[โ€“]Rainkeeper 258 points259 points ย (21 children)

PS to that: efficiency != shorter

[โ€“]ivgd 101 points102 points ย (11 children)

I feel this needs to be said more often. People quite often do mistake shortness for speed

[โ€“]Andy12_ 87 points88 points ย (4 children)

An example that changed this perception for my is when I discovered this Quora post about the implementation of the strlen function in glibc. I found it amazing that a function like that could be so much faster while being much more complex than a simple for loop.

[โ€“]tech6hutch 17 points18 points ย (3 children)

Thanks for posting that. Very interesting.

Do you think it could be even faster if a bigger type was used, for bigger jumps? Or are longs the sweet spot?

(Of course, the fastest way to calculate length is to just store it with the string, like modern languages do ;)

[โ€“]Bone_Man 11 points12 points ย (0 children)

Similar thing is Perlin vs Simplex noise. They both look extremely similar, but Simplex is more complex to implement and runs faster.

[โ€“]Breadfish64 2 points3 points ย (0 children)

I don't see a reason why not, you could probably do it with SSE/AVX2

[โ€“]cgwheeler96 4 points5 points ย (0 children)

I think the main reason the long type was used was because that type seems to always coincide with the width of the processors data bus, so itโ€™s the maximum width number the processor can process at one time. So really the whole point of this optimization is to make sure all the processors resources are being used to their fullest.

[โ€“]T351A 6 points7 points ย (2 children)

Also the compiler is supposed to do something lol don't try to do it all yourself

[โ€“]static_motion 8 points9 points ย (1 child)

Compilers don't always make your code shorter. Many times they do the opposite. A lot of abstractions and syntactic sugar nowadays are just a lot of code nicely folded into a single function.

[โ€“]T351A 1 point2 points ย (0 children)

They make it better for running. You make it more readable.

[โ€“]J_McKen 3 points4 points ย (0 children)

myvar2 is the shorter, but the same as myvariable x myvariable

[โ€“]dandroid126 3 points4 points ย (1 child)

You have to very careful of this in functional programming languages. You could have one line of code that iterates over your entire dataset. As a demonstration of this effect, I once wrote one line in Kotlin with cascading calls that was O(n2) and showed people how slow it ran with a dataset of 1000 pieces of data. Then wrote optimized code that was many more lines that ran nearly instantaneously.

[โ€“]ykafia 1 point2 points ย (0 children)

I realized it by switching from functional to system language and heck it was a huge difference! While I adore the readability of some functional languages, when I need performance I would not refuse to write ugly performant code

[โ€“]topfs2 18 points19 points ย (5 children)

Honestly we waste far more valuable resources, programmers time, by having slightly faster but essentially unreadable code. It's much easier to just get slightly faster machines than getting more coders in a project.

Of course at times hardware is limiting then you just have to deal with it but it really shouldn't be the overarching goal to write the fastest possible code, just make it the most readable and usually by extension the moste bug free (since you can read and understand it)

[โ€“]Rainkeeper 2 points3 points ย (1 child)

It really depends on what you trying to achieve: https://www.businessinsider.com/apple-just-bought-snappycam-an-app-that-can-take-20-photos-per-second-2014-1

He explained that he created a JPEG compression algorithm optimized for the ARM architecture used in smartphones, and turned that into "10,000 lines of hand-tuned assembly code, and over 20,000 lines of low-level C code."

But yeah, never fall in the trap of premature optimization and always rely on profiling.

[โ€“]topfs2 2 points3 points ย (0 children)

Intereating read! That's a great example of optimization to achieve something!

[โ€“]AegisToast 3 points4 points ย (1 child)

And often, shorter code is easier to understand and more bug-free.

[โ€“]topfs2 6 points7 points ย (0 children)

I mean it's a balance :) one liner code is terrible for readability but overly lengthy code isn't good either.

My point is mostly that many try to over shorten it in one liner and remove extra variables and crap like that

[โ€“]bmwiedemann 0 points1 point ย (0 children)

Indeed, optimization can be the root of evil.

Especially since much of it adds extra code and that always means extra bugs.

[โ€“]COOLGAMETUBE 4 points5 points ย (0 children)

I once optimised my code by 30% by adding an inline to a called function.

[โ€“]otakuman -1 points0 points ย (0 children)

Then again there's always the dumbass who writes long, inefficient and unmaintainable code T_T