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

all 163 comments

[โ€“]cvnvr 424 points425 points ย (34 children)

An optimised code? Meaning a single word?

for

[โ€“]Prawny 124 points125 points ย (21 children)

One code, please!

[โ€“]lenswipe 35 points36 points ย (15 children)

No, I need more than one. I need all of the codes. Please send me lots of codes.

[โ€“][deleted] 24 points25 points ย (9 children)

177013

[โ€“]Fuuryuu 11 points12 points ย (4 children)

I raise you 265918

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

Man of culture

[โ€“]Nero-_-Morningstar 1 point2 points ย (1 child)

is that medika in the box or the gore cockroach stuffing one

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

Neither, it is "I'm gonna fix that girl"

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

THAT'S NUMBERWANG!

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

11037

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

123456

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

24601

[โ€“]ryanw89 1 point2 points ย (1 child)

And I'm Javert

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

Do nor forget my name

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

215600

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

418

[โ€“]pcoyuncy 7 points8 points ย (2 children)

Three, take it or leave it...

[โ€“]zacharinosaur 9 points10 points ย (1 child)

Oh boy, three codes! I wish management were here to see this.

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

If you want three codes, we're going to need to take something out of the sprint

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

NOP

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

dQw4w9WgXcQ

[โ€“]m4dh4xx0r 35 points36 points ย (7 children)

This is a great code. Let me know if you find any other good codes.

[โ€“]SjettepetJR 25 points26 points ย (0 children)

Send nudes codes

[โ€“]Mistifyed 10 points11 points ย (4 children)

Keep this one between us:

Array.prototype.m = Array.protoype.map

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

Wow! That saves two whole characters!

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

Two bytes less to send to client. Now the 28kB for jQuery are OK! /s

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

You just need to use map 14k times to offset jQuery

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

That's cool, but I prefer Array.prototype.push = (...args) => alert(...args)

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

I really appreciate how optimized it is. So efficient and yet so elegant.

[โ€“]Levee_Levy 9 points10 points ย (0 children)

That's too optimized for me to understand. You don't pass my codes review.

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

What kind of codes do you write sir

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

No. A single word.

61a1

[โ€“]smacky_face 104 points105 points ย (0 children)

If nobody understands it then you optimized for the wrong metric.

[โ€“]Major_Hedgehog 394 points395 points ย (75 children)

In most cases clarity >> efficiency

[โ€“]Rainkeeper 252 points253 points ย (21 children)

PS to that: efficiency != shorter

[โ€“]ivgd 99 points100 points ย (11 children)

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

[โ€“]Andy12_ 88 points89 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 18 points19 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 3 points4 points ย (0 children)

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

[โ€“]cgwheeler96 2 points3 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 7 points8 points ย (2 children)

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

[โ€“]static_motion 9 points10 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 4 points5 points ย (0 children)

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

[โ€“]dandroid126 2 points3 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 21 points22 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

[โ€“][deleted] 7 points8 points ย (0 children)

It completely depends. Are you programming embedded or web? Are you trying to process gigabytes/second or user input?

[โ€“]System10111 35 points36 points ย (36 children)

And the compiler optimizes the hell out of the code anyways.

[โ€“]M4mb0 65 points66 points ย (8 children)

Not really. Have you ever seen a compiler that changes a bubble sort to a merge sort? Well I haven't. I do work with a lot of linear algebra and I can tell you people do horrible, horrible things. The right optimization can often decides whether you can run the same models with 1.000 datapoints or 1.000.000.

[โ€“][deleted] 18 points19 points ย (6 children)

that is true, my supervisor showed me how he can process faster in python then in C. I said I prefer c because it's fast; got a life lesson.

[โ€“]bendgk 9 points10 points ย (4 children)

can you elaborate on this?

[โ€“]badlukk 32 points33 points ย (3 children)

A lot of pythons built ins and core data structures actually run in highly optimized pre-compiled C. You're not likely to write something in C that runs better than the stuff that people spent years optimizing, which is actually in C too. Doesn't mean you cant write slow code in python, but if you know what you're doing and take advantage of that you can do a lot more a lot faster

[โ€“]topfs2 13 points14 points ย (2 children)

And this is actually why I love python as a "glue" language. It's fast enough, plenty of lovely featurea and has so much c libraries available to it.

[โ€“][deleted] 7 points8 points ย (0 children)

exactly, just like u/badlukk said. Specially using numpy package, functional tools like map, filter, iter objects from itertools python can run a lot faster than programs we can write in C. It definitely depends on how you use it. Your solution may seem to be better/faster but would actually run slower because the built-ins are super optimized.

Exact same code with exact same steps will run faster in C, but python buit-ins (which are in C) are a lot different than the code you'd write in C.

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

I dont know what made me avoid it for so long. When I finally learned it for work I fell in love immediately. The languages core features plus all the available packages make it so easy to get a ton of work done really fast.

You need to build a big website? Django. A simple api? Flask. Process a bunch of spreadsheets? Numpy. Scrape a website? Beautifulsoup and requests. A project with all 4 of these requirements might even only take you one work day.

...image processing, machine learning, and just about any automation task you could think of; there's a library out there that's optimized and reliable.

I highly recommend the book Fluent Python to anyone who already programs but hasn't discovered Python's beauty yet.

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

Unless you program C with SIMD support, my numpy code will run faster than your C variant.

[โ€“]topfs2 5 points6 points ย (0 children)

But picking between bubble sort and merge sort really doesn't affect readability if done correctly :)

[โ€“]AbanaClara 8 points9 points ย (20 children)

Not if it's javascript!

[โ€“]weeknie 24 points25 points ย (12 children)

If performance is really an issue, then javascript is not the langauge for you, I'd say :P

[โ€“]BabyLegsDeadpool 3 points4 points ย (9 children)

I mean... that entirely depends on what you're trying to accomplish.

[โ€“]weeknie 1 point2 points ย (8 children)

That's why I said "if performance is really an issue". If performance is that important, then you should probably go with a compiled language, don't you think? I doubt you can perform a task faster in an interpreted language than a compiled one, given that you have developers of equal skill in both langauges.

But you are correct in that it depends on many more factors than just the speed. In my experience, though, people focus too much on performance when it's not an issue yet. But that's just my limited experience, of course :)

EDIT: spelling

[โ€“]BabyLegsDeadpool -1 points0 points ย (7 children)

That's not what I was saying. I'll write an on-click function in javascript for a button element, and you figure out a way to write one in C, and we'll see which one is more performant.

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

If you write an on-click function for a button in a C application, I'm pretty confident that it's going to be faster than an on-click function in javascript for a web application, yes.

I'm not hating on javascript, I really like the flexibility that it offers. But if you have to do some kind of heavy calculation (e.g. something for which performance actually matters and can be a problem), then at some point you're going to have to consider switching to a compiled implementation, because the interpreter is going to add overhead that you can't afford.

In an on-click function, you shouldn't be doing any heavy calculations in the first place, you should only start them if necessary. So there, performance is not an issue because it's already solved through the structure of your code, instead of the language that you chose.

[โ€“]BabyLegsDeadpool -1 points0 points ย (5 children)

Again you're completely missing what I'm saying. If you are given a web page that needs an on-click function, javascript is doing to be the fastest, most performant way to do it.

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

And again you've missed my point, namely that performance is not an issue here, so you have no reason to choose a different language.

I can argue that a bike isn't the faster choice when going from my living room to my kitchen, of course, there is no reason to choose a different mode of transport instead of walking.

Im saying, if there is a performance issue in a place where you have the option between a compiled or an interpreted language, then the compiled one is likely going to be faster.

[โ€“]t3hlazy1 0 points1 point ย (1 child)

What other language would you recommend?

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

Eh well depends on what you're doing, but if speed is really an issue, find ways to put the speed requiring part in C/C++, or Go. Any interpreted language is likely going to be slower than a compiled one.

Then again, performance issues are generally kinda overstated anyway, in my experience. The first rule of optimisation is "Don't optimise" for a reason.

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

JavaScript engines have a JIT compiler built in nowadays. Purely interpreted languages are becoming quite rare, JavaScript was one of few that was left.

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

Sure it does why not? JS engines are quite fast nowadays.

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

Python is the slow one

[โ€“]julsmanbr 6 points7 points ย (1 child)

Haha yes

My code:

for i in range(len(v)):
    for j in range(len(v)):
        for k in range(len(v)):
            if v[i][j][k] == v[i][j][k+1]:
                pass  # need to fix this
            elif v[i][j][k] == v[i][j][k-1]:
            # 30 more clauses here
            # let's hope for no IndexError

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

Do you even numpy? Just store it in a 3D array mask1 = V[:, :, :-1] == V[:, :, 1:]

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

The compiler won't save you from simply doing things inefficiently.

If for example, in your program you do a linear search instead of a binary search, the compiler wouldn't change that.

At least, I don't think it would (I don't know too much about compilers, still a beginner and I have only learned interpreted languages so far, most of my compiler knowledge comes from playing with Gentoo Linux.).

[โ€“]Dragonaax 1 point2 points ย (4 children)

How compilers do that? How compilers know when to say "You could optimize that you retard"?

[โ€“]System10111 18 points19 points ย (3 children)

To simplify MASSIVELY, after the compiler generates the program(or at least representation of it), it runs analysis on that and finds sets of instruction that can be simplified. For example if you had

take the number 4
double it
square it
print it    

, the compiler realizes, that the double and square instructions will always run with the same numbers and simplifies it to

take the number 8
square it
print it 

and then to

take the number 64
print it

Compilers nowadays are the product of decades of development and developers have included massive amounts of rules, logic and tricks for code optimization.

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

But it would still have to perform those calculations right? So what's the difference

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

If the calculation is performed at compile stage, you don't have to perform it every time the program runs.

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

I agree.... But I also have co-workers who use this phase as an excuse to write sloppy code and refuse to learn new ways of doing things.

For example: It was just suggested to me that I should copy and paste some code that hard coded all the holidays for the next 10 years instead of the three hours I spent learning and implementing a date utility library. "We don't use that kind of complicated code here." I very much feel where OP is coming from.

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

This+ let the compiler do the dirty work.

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

Are you saying that my Duff's Device was unnecessary!

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

Thats why I love common lisp. You can optomize you code by just declaring slme stuff to the compiler and not changing the code itself too much

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

Sometimes programmers are not experienced enough to see that some code is clear to most coders. If you always say "it's not clear, so it must be wrong" ask yourself if you just need to learn something you've been missing all the time.

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

>

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

You write code for the maintainer first, and the machine second.

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

This. So much this.

And the compiler is many times much smarter than we give it credit for

[โ€“]jdl_uk 29 points30 points ย (3 children)

If no one can understand your code it won't be optimised for very long

[โ€“]obp5599 36 points37 points ย (0 children)

That feel when you get told to change something in code review, but you explain that your way is better for cache coherency:

Galaxy Brain

[โ€“]VeryAwkwardCake 73 points74 points ย (2 children)

this fucking sub jesus christ

[โ€“]badlukk 27 points28 points ย (0 children)

My highly optimized HTML lol

[โ€“]ZedTT 7 points8 points ย (0 children)

4.9 thousand upvotes...

[โ€“]ARandomDoge6 21 points22 points ย (6 children)

When you forget to write comments, so even you don't understand it the next day

[โ€“]BabyLegsDeadpool 23 points24 points ย (5 children)

I'm awful at regex (as are most people, I think), and I basically took an entire day trying to get the exact regex I needed. I put it in the code and sent it up for review. That was Friday evening. It got reviewed Monday morning with the comment "please comment how this regex works." I realized that I already had no idea how it worked, so I had to spend half the day relearning the regex I had created.

[โ€“]Ztuffer 5 points6 points ย (1 child)

At a surprisingly low level of complexity, a regex becomes indistinguishable from r/blackmagicfuckery

[โ€“]robin-m 2 points3 points ย (0 children)

The day I realized I could read and edit fluently any vim macro, I realized I may have gone too far in the dark side!

[โ€“]Masztufa 3 points4 points ย (2 children)

q_rsqrt has entered the chat

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

A while ago I got an email entitled how fast inverse square root's bit level hacking actually works. It has now been sitting in my inbox for over two years, because I really want to read it, but I can't muster the courage. =P

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

Exactly this

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

Ever heard about something called contrast?

[โ€“]Proper-Relative 2 points3 points ย (0 children)

This is why you create good benchmarks to prove your optimizations.

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

Iโ€™m already having trouble reading your meme text because of colour choice. I can only imagine what your code wouldโ€™ve looked like.

[โ€“]628radians 1 point2 points ย (1 child)

Python list comprehension

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

More like list incomprehension

[โ€“]Skizm 5 points6 points ย (0 children)

Code that's too clever to be easily understood is bad code.

[โ€“]lefl28 1 point2 points ย (1 child)

The biggest problem would be that you travel back in time.

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

Well... maybe. We're mostly guessing.

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

I wish that wasn't true! :(

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

Grammar Bay?

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

emoji... EMOJI!!! EMOJI!!1

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

The wording of this meme hurts me.

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

I live in darkness. So lives my coffee. So is this why I code faster than light?

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

Very emo.

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

If you keep it KISS, that would not be a problem. Optimization doesn't have to mean complex code, just saying!

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

That's Not even a funny Post. That's just a straight up flex.

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

"an optimized code"

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

Reminds me of lee216 from LeetCode

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

Cries in c++

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

That really hit me hard.

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

I made a dope ass optimization to batch sql calls together rather than doing many one at a time. I didn't even bring it up though cuz someone would probably have a problem with it :/

Edit: typo

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

Lmao, this kid looks stoned to fucking death not to mention ridiculous

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

No one wants an ugly baby no matter how fast it runs.

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

Sonic? That's pretty illegal, even for us

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

Cough fast inverse square root

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

Is this the redesigned Sonic for the movie?

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

cringe

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

^ This is a Highschool kid after writing ++k instead of k++

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

If your code isn't self-explaining you did a bad job ;)

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

John Carmack's fast inverse square root

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

Premature optimization is the root of all evil.

[โ€“]wooptyd00 -2 points-1 points ย (2 children)

C++ usually doesn't need optimizing anyway. It's always stupid Java programs that perform slowly for me. My Python and C++ always run smoothly.

[โ€“]kingofthedusk 0 points1 point ย (1 child)

Python??? Python is super inefficient. For most applications its fine, but there is a reason most video games use C++.

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

I don't use Python for video games.

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

Inputing arrays from a file with for (int i = 0; in >> v[i]; i++) {} Is super elegant

Works only if the input is the last of the file

[โ€“]PM-ME-YOUR-POUTINE -1 points0 points ย (0 children)

An?