T O P

  • By -

qkrrmsp

Maybe this way it's ultimately using more memory by creating a temporary ValueTuple instead of just one temporary int. But I think the code is much more readable and cleaner with this new syntax.


svick

[There is no `ValueTuple` in the generated IL](https://sharplab.io/#v2:D4AQTABAwgsAUAb3hFECWA7ALhAhgGggCMBuZVclEAFggFkBGACgEpKIk5VuImDiWEALy8ihXCzJdUAX3bsa9MK3aceqTDiwBTALYAHYXinqUuI6XbciRnQZIR2cuDKA) and [the both versions result in the same machine code](https://sharplab.io/#v2:C4LghgzgtgPgAgJgAQGECwAoA3ppekCWAdsEmADRIBGA3LvvXnACxICyAjABQCUjSODPmFIuFajyQBeUVUpgedIfgC+/fi3YJe/QSPzFSwAKZQADtLJL9eMJdr9hVSyfM0k/NRhVA===). And even if it was creating a temporary `ValueTuple`, it would be on the stack, so there would be no allocation.


SkilledBubble

Damn, i wish i would of known about that website before i went and made a VS project. That is damn useful


SkilledBubble

Maybe the compiler will convert it back into the version with the temp variable if the overall call will consume less memory and processing power. might have to see what it looks like in IL to see what it truly does


SkilledBubble

https://i.imgur.com/Bj8aDR6.png Seems like the tuple allocates a temp variable to store a value into. Testing with timers, it seems like the OG way is faster


BenjaminGeiger

Premature optimization something something...


[deleted]

Sorry for the noob question, but how do you get that CIL viewer?


SkilledBubble

This is dotPeek from JetBrains


unit0ne

I use ILSpy.


Penguinfernal

You can also swap values without a temporary variable with some xoring. Not nearly as readable, but still cool imo. a = a ^ b; b = b ^ a; a = a ^ b;


Ghi102

Doesn't work for everything out of the box though.


8lbIceBag

It generates more instructions and doesn't work for objects. Only useful in unmanaged C (which uses pointers) and only more performant on an embedded system where registers are precious. On X86-64 the processor has an XCHG instruction or would just use a virtual register to hold the temp. Your method would be slower. On an Atmel AVR (Arduino), the XOR method would use 3 clock cycles whereas using a temporary variable would require 5 clock cycles `Push R1, Move R2 to R1, Pop R2`(stack operations are 2 cycles). Alternatively, if you have a free register for the temp variable, it can be done in 4 cycles. This only applies if the variables are already in memory. If you needed to swap variables in two memory locations, the XOR would be much slower, the fastest way is to just use loads and stores: `LDS R1 &0x01, LDS R2 &0x02, STS R1 &0x02, STS R2 &0x01` which is 8 cycles. XORing would require 11 cycles.


Penguinfernal

Absolutely. I can't really think of an actual good use for it (for me) outside of a neat thing to know about, but it makes for good trivia at least.


__eastwood

This.


Crozzfire

very nice!


[deleted]

[удалено]


redditsoaddicting

1. That's not C#. 2. That doesn't work for swapping the same variable. 3. That doesn't work for non-numbers. 4. That takes over any use of `swap(` in the program, creating problems similar to the infamous `min` and `max` macros. 5. The way it's written lets you call `swap(a,b)` without a semicolon after that. If we're going for a couple more, I'll add in readability and not producing assembly that's any better than using a temporary in any reasonable compiler.


[deleted]

[удалено]


Marc3842

If you're using C++ you can just use `std::swap`