4 ms·
Or, in Python a, b = b, a Or Rust: (a, b) = (b, a);
by milliams 5y ago
Or, in Python
a, b = b, a
Or Rust:
(a, b) = (b, a);
- jslaby 5y agoor with a static variable
- Thiez 5y agoCongratulations, your program is now thread-unsafe because you thought using a temporary variable is expensive :-p using mutable static variables is a terrible idea in most situations.
- paxys 5y agoThese aren't algorithms. The language is still doing something under the hood to do the swap.
- manojlds 5y agoWas that the problem statement? Come up with an algorithm to swap two numbers?
- lanna 5y agoSome people seem to forget that in high-level programming languages, lines of code seldom map 1-to-1 to processor instructions.
- baseballdork 5y agoIndeed, both the algorithm in the article, the rust suggestion, and even the naive solution compile to the same instructions[1]. [1] - https://godbolt.org/z/1P98ss37P https://godbolt.org/z/1P98ss37P
- abainbridge 5y agoThat uses 4 registers and a stack!
- Thiez 5y agoYou can't do better with a non-inlined function.
- happytoexplain 5y agoThe downvotes on the OP comment and the negativity in the replies is totally baffling to me. The OP's post was interesting and relevant, despite being non-algorithmic examples. Non-algorithmic examples are not required by the title of the article, and are only implicitly the topic of the article's body, and even if it were explicit, that still wouldn't render the OP comment unrelated. It would have been enough to simply talk about the difference between an algorithmic and language-feature solution without implying something is wrong with even posting the latter.
- tialaramex 5y agoAnd on a modern CPU the machine code does not map 1-to-1 to what actually happens when this is executed, the phrase "out-of-order execution" ought to give away that even though your machine code says A, then B, then C, the CPU may decide it was better to do A, then C, then B instead. Express your actual intent, if your chosen language has a "swap" intrinsic, use it, if not write the swap in a natural and idiomatic way, and let other people do the lifting.
- kzrdude 5y agoAnd then the compiled code is doing something under the hood to do whatever the language thought it said. And then the cpu is doing something under the hood to do whatever the assembly code thought it said.
- bastawhiz 5y agoWhere do you think the intermediate results of doing math on the two variables are kept?
- kmill 5y agoFor what it's worth, the Python example isn't too relevant since it's hiding a number of intermediate variables (that said, swapping without intermediate variables tends to be more of a curiosity anyway -- and a motivated person can "well actually" the whole concept into oblivion). What it does is push a and b onto the interpreter stack, then (in the version of Python 3 I'm running, though the more recent one should be similar) it pops those into two local C variables and pushes them in a different order, then it pops them and stores them into local python variables. import dis def swap(a, b): a, b = b, a return a dis.dis(swap) Output: 4 0 LOAD_FAST 1 (b) 2 LOAD_FAST 0 (a) 4 ROT_TWO 6 STORE_FAST 0 (a) 8 STORE_FAST 1 (b) 5 10 LOAD_FAST 0 (a) 12 RETURN_VALUE Implementation of ROT_TWO https://github.com/python/cpython/blob/bc85eb7a4f16e9e2b6fb713be2466ebb132fd7f2/Python/ceval.c#L1826 https://github.com/python/cpython/blob/bc85eb7a4f16e9e2b6fb7...
- w3news 5y agoJS: [b,a] = [a,b];
- Adverblessly 5y agoEven in C++ std::tie(a, b) = std::tuple(b, a);
- ncmncm 5y agoOr, you know, std::swap(a, b);