124
sugoi
6y

Who here is a great programmer?

Comments
  • 10
  • 6
    @MisterArie Well after reading your rant's you seem to be.
  • 14
    Im not sure but I think the great option is slower in execution than the others.
  • 16
    This is code porn!
  • 5
    @Codex404 It does looks it will take more memory but not time (I guess).
  • 6
    @shaikadil writing an array to memory is relatively heavy compared to a if statement as far as I know, but it probably depends on the language as well
  • 78
    How about just:
    x % 2 == 0 ? 'even' : 'odd'

    And now we have a oneliner.
  • 9
    @Codex404 that array is compile time constant, so it will probably be compiled into the executable, which means it will probably be just as fast as the other options (at least on compiled languages).

    The other two options, however, might need some kind of branching, because the compiler might not know how to optimize the if, whereas the las one should be an and (mod 2), load and call. So it will probably be like one or two cpu cycles faster.
  • 26
    Fucking shite programmers the whole bunch. They don't indent in their blocks
  • 1
    @aritzh
    Still the CPU needs to access the array, which might be more costly operation.

    The compilation point is valid and the execution time might vary by how the code is optimized. In this code example the difference is nevertheless non-existent :D unless you need to do the operation 100000 times in a second.
  • 1
    @Codex404 You can make array initialized at compile time in some languages like c++ hence it is known how it will be at runtime. In that way I think there will be no cost on runtime.
  • 9
    @okkimus @Codex404 Actually, I will have to rectify, just looking at the assembly (https://godbolt.org/g/jQ1x6H), it looks like the ternary operator requires the least amount of assembly instructions. Would have to do a proper benchmark to know which would be faster in practice, but it is interesting to see how compilers can optimize some things better than others.
  • 3
    @aritzh
    Wow that's interesting! Kudos to you.

    I actually thought that ternary would be just be syntactic sugar for if-else clause. Never really looked into compilers or C++.
  • 2
    @Phippsaurus finally, in the perfect form.
  • 5
    Good programer is already odd. Add an m to make it even.
  • 1
    That indentation triggers me on a molecular level.
  • 2
  • 4
    Also the correct solution is to download a jQuery plugin and do:

    console.log($(value).isEven());
  • 0
    @Phippsaurus I'm curious, is x&1==x faster than x|1>x?
  • 1
    I send a ternary operator to cout. Does that count?
  • 3
    @hch11 it couts
  • 1
  • 0
    WHAT IS X??? Programming is not algebra!
  • 4
    @Pogromist x is input variable. Programming is really related to maths(including Algebra). Same as we have variables in algebraic expressions we have variables which change throughout the program.
  • 1
    Not need an array
  • 3
    @okkimus why not cout<<x&1?"odd":"even"
  • 2
    @gitlog edit: same code size in assembly :)
    @okkimus
  • 0
    In this case the best solution is the one that keeps raising the price of RAM, and since I'm poor but moralistic I suggest to optimize it.
Add Comment