<p>We have been working with CVS for years, and frequently find it useful to "sticky" a single file here and there.</p>

<p>Is there any way to do this in subversion, specifically from TortoiseSVN?</p>

[{'answer_id': 36910, 'author': 'caramelcarrot', 'author_id': 3877, 'author_profile': 'https://Stackoverflow.com/users/3877', 'pm_score': 7, 'selected': False, 'text': '<p>Number 2 is often quoted as being the "clever" way of doing it. It is in fact most likely slower as it obscures the explicit aim of the programmer - swapping two variables. This means that a compiler can\'t optimize it to use the actual assembler ops to swap. It also assumes the ability to do a bitwise xor on the objects.</p>\n\n<p>Stick to number 1, it\'s the most generic and most understandable swap and can be easily templated/genericized.</p>\n\n<p>This wikipedia section explains the issues quite well:\n<a href="http://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice" rel="noreferrer">http://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice</a></p>\n'}, {'answer_id': 36922, 'author': 'Sander', 'author_id': 2928, 'author_profile': 'https://Stackoverflow.com/users/2928', 'pm_score': 4, 'selected': False, 'text': '<p>The first is faster because bitwise operations such as xor are usually very hard to visualize for the reader.</p>\n\n<p>Faster to understand of course, which is the most important part ;)</p>\n'}, {'answer_id': 36933, 'author': '17 of 26', 'author_id': 2284, 'author_profile': 'https://Stackoverflow.com/users/2284', 'pm_score': 3, 'selected': False, 'text': "<p>The only way to really know is to test it, and the answer may even vary depending on what compiler and platform you are on.  Modern compilers are <em>really</em> good at optimizing code these days, and you should never try to outsmart the compiler unless you can prove that your way is really faster.</p>\n\n<p>With that said, you'd better have a damn good reason to choose #2 over #1.  The code in #1 is far more readable and because of that should always be chosen first.  Only switch to #2 if you can prove that you <em>need</em> to make that change, and if you do - comment it to explain what's happening and why you did it the non-obvious way.</p>\n\n<p>As an anecdote, I work with a couple of people that <em>love</em> to optimize prematurely and it makes for really hideous, unmaintainable code.  I'm also willing to bet that more often than not they're shooting themselves in the foot because they've hamstrung the ability of the compiler to optimize the code by writing it in a non-straightforward way.</p>\n"}, {'answer_id': 36942, 'author': 'Ant', 'author_id': 2289, 'author_profile': 'https://Stackoverflow.com/users/2289', 'pm_score': 8, 'selected': True, 'text': '<p>The XOR method fails if a and b point to the same address.  The first XOR will clear all of the bits at the memory address pointed to by both variables, so once the function returns (*a == *b == 0), regardless of the initial value.</p>\n\n<p>More info on the Wiki page:\n<a href="http://en.wikipedia.org/wiki/XOR_swap_algorithm" rel="noreferrer">XOR swap algorithm</a></p>\n\n<p>Although it\'s not likely that this issue would come up, I\'d always prefer to use the method that\'s guaranteed to work, not the clever method that fails at unexpected moments.</p>\n'}, {'answer_id': 36948, 'author': 'Tim Ring', 'author_id': 3685, 'author_profile': 'https://Stackoverflow.com/users/3685', 'pm_score': 0, 'selected': False, 'text': '<p>If you can use some inline assembler and do the following (psuedo assembler):</p>\n\n<pre><code>PUSH A\nA=B\nPOP B\n</code></pre>\n\n<p>You will save a lot of parameter passing and stack fix up code etc. </p>\n'}, {'answer_id': 37084, 'author': 'Nir', 'author_id': 3509, 'author_profile': 'https://Stackoverflow.com/users/3509', 'pm_score': 3, 'selected': False, 'text': "<p>You are optimizing the wrong thing, both of those should be so fast that you'll have to run them billions of times just to get any measurable difference.</p>\n\n<p>And just about anything will have much greater effect on your performance, for example, if the values you are swapping are close in memory to the last value you touched they are lily to be in the processor cache, otherwise you'll have to access the memory - and that is several orders of magnitude slower then any operation you do inside the processor.</p>\n\n<p>Anyway, your bottleneck is much more likely to be an inefficient algorithm or inappropriate data structure (or communication overhead) then how you swap numbers.</p>\n"}, {'answer_id': 40336, 'author': "Andrew O'Reilly", 'author_id': 3692, 'author_profile': 'https://Stackoverflow.com/users/3692', 'pm_score': 2, 'selected': False, 'text': '<p>To answer your question as stated would require digging into the instruction timings of the particular CPU that this code will be running on which therefore require me to make a bunch of assumptions around the state of the caches in the system and the assembly code emitted by the compiler. It would be an interesting and useful exercise from the perspective of understanding how your processor of choice actually works but in the real world the difference will be negligible.</p>\n'}, {'answer_id': 44911, 'author': 'paperhorse', 'author_id': 4498, 'author_profile': 'https://Stackoverflow.com/users/4498', 'pm_score': -1, 'selected': False, 'text': "<p>I just placed both swaps (as macros) in hand written quicksort I've been playing with. The XOR version was much faster (0.1sec) then the one with the temporary variable (0.6sec). The XOR did however corrupt the data in the array (probably the same address thing Ant mentioned).<p>\nAs it was a fat pivot quicksort, the XOR version's speed is probably from making large portions of the array the same. I tried a third version of swap which was the easiest to understand and it had the same time as the single temporary version.\n<pre><code>\nacopy=a;\nbcopy=b;\na=bcopy;\nb=acopy;\n</code></pre><p>\n[I just put an if statements around each swap, so it won't try to swap with itself, and the XOR now takes the same time as the others (0.6 sec)]</p>\n"}, {'answer_id': 45512, 'author': 'Skizz', 'author_id': 1898, 'author_profile': 'https://Stackoverflow.com/users/1898', 'pm_score': 5, 'selected': False, 'text': "<p>On a modern processor, you could use the following when sorting large arrays and see no difference in speed:</p>\n\n<pre><code>void swap (int *a, int *b)\n{\n  for (int i = 1 ; i ; i &lt;&lt;= 1)\n  {\n    if ((*a &amp; i) != (*b &amp; i))\n    {\n      *a ^= i;\n      *b ^= i;\n    }\n  }\n}\n</code></pre>\n\n<p>The really important part of your question is the 'why?' part. Now, going back 20 years to the 8086 days, the above would have been a real performance killer, but on the latest Pentium it would be a match speed wise to the two you posted.</p>\n\n<p>The reason is purely down to memory and has nothing to do with the CPU.</p>\n\n<p>CPU speeds compared to memory speeds have risen astronomically. Accessing memory has become the major bottleneck in application performance. All the swap algorithms will be spending most of their time waiting for data to be fetched from memory. Modern OS's can have up to 5 levels of memory:</p>\n\n<ul>\n<li>Cache Level 1 - runs at the same speed as the CPU, has negligible access time, but is small</li>\n<li>Cache Level 2 - runs a bit slower than L1 but is larger and has a bigger overhead to access (usually, data needs to be moved to L1 first)</li>\n<li>Cache Level 3 - (not always present) Often external to the CPU, slower and bigger than L2</li>\n<li>RAM - the main system memory, usually implements a pipeline so there's latency in read requests (CPU requests data, message sent to RAM, RAM gets data, RAM sends data to CPU)</li>\n<li>Hard Disk - when there's not enough RAM, data is paged to HD which is really slow, not really under CPU control as such.</li>\n</ul>\n\n<p>Sorting algorithms will make memory access worse since they usually access the memory in a very unordered way, thus incurring the inefficient overhead of fetching data from L2, RAM or HD.</p>\n\n<p>So, optimising the swap method is really pointless - if it's only called a few times then any inefficiency is hidden due to the small number of calls, if it's called a lot then any inefficiency is hidden due to the number of cache misses (where the CPU needs to get data from L2 (1's of cycles), L3 (10's of cycles), RAM (100's of cycles), HD (!)).</p>\n\n<p>What you really need to do is look at the algorithm that calls the swap method. This is not a trivial exercise. Although the Big-O notation is useful, an O(n) can be significantly faster than a O(log n) for small n. (I'm sure there's a CodingHorror article about this.) Also, many algorithms have degenerate cases where the code does more than is necessary (using qsort on nearly ordered data could be slower than a bubble sort with an early-out check). So, you need to analyse your algorithm and the data it's using.</p>\n\n<p>Which leads to how to analyse the code. Profilers are useful but you do need to know how to interpret the results. Never use a single run to gather results, always average results over many executions - because your test application could have been paged to hard disk by the OS halfway through. Always profile release, optimised builds, profiling debug code is pointless.</p>\n\n<p>As to the original question - which is faster? - it's like trying to figure out if a Ferrari is faster than a Lambourgini by looking at the size and shape of the wing mirror.</p>\n"}, {'answer_id': 45551, 'author': 'Harry', 'author_id': 4704, 'author_profile': 'https://Stackoverflow.com/users/4704', 'pm_score': 3, 'selected': False, 'text': '<p>For those to stumble upon this question and decide to use the XOR method.  You should consider inlining your function or using a macro to avoid the overhead of a function call:</p>\n\n<pre><code>#define swap(a, b)   \\\ndo {                 \\\n    int temp = a;    \\\n    a = b;           \\\n    b = temp;        \\\n} while(0)\n</code></pre>\n'}, {'answer_id': 46123, 'author': 'Skizz', 'author_id': 1898, 'author_profile': 'https://Stackoverflow.com/users/1898', 'pm_score': 4, 'selected': False, 'text': '<p>Regarding @Harry:\nNever implement functions as macros for the following reasons:</p>\n\n<ol>\n<li><p>Type safety. There is none. The following only generates a warning when compiling but fails at run time: </p>\n\n<pre><code>float a=1.5f,b=4.2f;\nswap (a,b);\n</code></pre>\n\n<p>A templated function will always  be of the correct type (and why aren\'t you treating warnings as errors?).</p>\n\n<p>EDIT: As there\'s no templates in C, you need to write a separate swap for each type or use some hacky memory access.</p></li>\n<li><p>It\'s a text substitution. The following fails at run time (this time, without compiler warnings):</p>\n\n<pre><code>int a=1,temp=3;\nswap (a,temp);\n</code></pre></li>\n<li><p>It\'s not a function. So, it can\'t be used as an argument to something like qsort.</p></li>\n<li>Compilers are clever. I mean really clever. Made by really clever people. They can do inlining of functions. Even at link time (which is even more clever). Don\'t forget that inlining increases code size. Big code means more chance of cache miss when fetching instructions, which means slower code.</li>\n<li><p>Side effects. Macros have side effects! Consider:</p>\n\n<pre><code>int &amp;f1 ();\nint &amp;f2 ();\nvoid func ()\n{\n  swap (f1 (), f2 ());\n}\n</code></pre>\n\n<p>Here, f1 and f2 will be called twice.</p>\n\n<p>EDIT: A C version with nasty side effects:</p>\n\n<pre><code>int a[10], b[10], i=0, j=0;\nswap (a[i++], b[j++]);\n</code></pre></li>\n</ol>\n\n<p>Macros: <a href="http://www.youtube.com/watch?v=jCLs0jv_Efk" rel="nofollow noreferrer">Just say no!</a></p>\n\n<p>EDIT: This is why I prefer to define macro names in UPPERCASE so that they stand out in the code as a warning to use with care.</p>\n\n<p>EDIT2: To answer Leahn Novash\'s comment:</p>\n\n<p>Suppose we have a non-inlined function, f, that is converted by the compiler into a sequence of bytes then we can define the number of bytes thus:</p>\n\n<pre><code>bytes = C(p) + C(f)\n</code></pre>\n\n<p>where C() gives the number of bytes produced, C(f) is the bytes for the function and C(p) is the bytes for the \'housekeeping\' code, the preamble and post-amble the compiler adds to the function (creating and destroying the function\'s stack frame and so on). Now, to call function f requires C(c) bytes. If the function is called n times then the total code size is:</p>\n\n<pre><code>size = C(p) + C(f) + n.C(c)\n</code></pre>\n\n<p>Now let\'s inline the function. C(p), the function\'s \'housekeeping\', becomes zero since the function can use the stack frame of the caller. C(c) is also zero since there is now no call opcode. But, f is replicated wherever there was a call. So, the total code size is now:</p>\n\n<pre><code>size = n.C(f)\n</code></pre>\n\n<p>Now, if C(f) is less than C(c) then the overall executable size will be reduced. But, if C(f) is greater than C(c) then the code size is going to increase. If C(f) and C(c) are similar then you need to consider C(p) as well.</p>\n\n<p>So, how many bytes do C(f) and C(c) produce. Well, the simplest C++ function would be a getter:</p>\n\n<pre><code>void GetValue () { return m_value; }\n</code></pre>\n\n<p>which would probably generate the four byte instruction:</p>\n\n<pre><code>mov eax,[ecx + offsetof (m_value)]\n</code></pre>\n\n<p>which is four bytes. A call instuction is five bytes. So, there is an overall size saving. If the function is more complex, say an indexer ("return m_value [index];") or a calculation ("return m_value_a + m_value_b;") then the code will be bigger.</p>\n'}, {'answer_id': 155888, 'author': 'Dan Lenski', 'author_id': 20789, 'author_profile': 'https://Stackoverflow.com/users/20789', 'pm_score': 2, 'selected': False, 'text': '<p>I would not do it with pointers unless you have to.  The compiler cannot optimize them very well because of the possibility of <a href="http://en.wikipedia.org/wiki/Pointer_aliasing" rel="nofollow noreferrer">pointer aliasing</a> (although if you can GUARANTEE that the pointers point to non-overlapping locations, GCC at least has extensions to optimize this).</p>\n\n<p>And I would not do it with functions at all, since it\'s a very simple operation and the function call overhead is significant.</p>\n\n<p>The best way to do it is with macros if raw speed and the possibility of optimization is what you require.  In GCC you can use the <code>typeof()</code> builtin to make a flexible version that works on any built-in type.</p>\n\n<p>Something like this:</p>\n\n<pre><code>#define swap(a,b) \\\n  do { \\\n    typeof(a) temp; \\\n    temp = a; \\\n    a = b; \\\n    b = temp; \\\n  } while (0)\n\n...    \n{\n  int a, b;\n  swap(a, b);\n  unsigned char x, y;\n  swap(x, y);                 /* works with any type */\n}\n</code></pre>\n\n<p>With other compilers, or if you require strict compliance with standard C89/99, you would have to make a separate macro for each type.</p>\n\n<p>A good compiler will optimize this as aggressively as possible, given the context, if called with local/global variables as arguments.</p>\n'}, {'answer_id': 190995, 'author': 'Dan Cristoloveanu', 'author_id': 24873, 'author_profile': 'https://Stackoverflow.com/users/24873', 'pm_score': 1, 'selected': False, 'text': '<p>In my opinion local optimizations like this should only be considered tightly related to the platform. It makes a huge difference if you are compiling this on a 16 bit uC compiler or on gcc with x64 as target.</p>\n\n<p>If you have a specific target in mind then just try both of them and look at the generated asm code or profile your applciation with both methods and see which is actually faster on your platform.</p>\n'}, {'answer_id': 615995, 'author': 'Trevor Boyd Smith', 'author_id': 52074, 'author_profile': 'https://Stackoverflow.com/users/52074', 'pm_score': 2, 'selected': False, 'text': '<p>All the top rated answers are not actually definitive "facts"... they are people who are speculating!</p>\n\n<p>You can definitively <em>know for a fact</em> which code takes less assembly instructions to execute because you can look at the output assembly generated by the compiler and see which executes in less assembly instructions!</p>\n\n<p>Here is the c code I compiled with flags "gcc -std=c99 -S -O3 lookingAtAsmOutput.c":</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nvoid swap_traditional(int * restrict a, int * restrict b)\n{\n    int temp = *a;\n    *a = *b;\n    *b = temp;\n}\n\nvoid swap_xor(int * restrict a, int * restrict b)\n{\n    *a ^= *b;\n    *b ^= *a;\n    *a ^= *b;\n}\n\nint main() {\n    int a = 5;\n    int b = 6;\n    swap_traditional(&amp;a,&amp;b);\n    swap_xor(&amp;a,&amp;b);\n}\n</code></pre>\n\n<p>ASM output for swap_traditional() takes >>> 11 &lt;&lt;&lt; instructions ( not including "leave", "ret", "size"):</p>\n\n<pre><code>.globl swap_traditional\n    .type   swap_traditional, @function\nswap_traditional:\n    pushl   %ebp\n    movl    %esp, %ebp\n    movl    8(%ebp), %edx\n    movl    12(%ebp), %ecx\n    pushl   %ebx\n    movl    (%edx), %ebx\n    movl    (%ecx), %eax\n    movl    %ebx, (%ecx)\n    movl    %eax, (%edx)\n    popl    %ebx\n    popl    %ebp\n    ret\n    .size   swap_traditional, .-swap_traditional\n    .p2align 4,,15\n</code></pre>\n\n<p>ASM output for swap_xor() takes >>> 11 &lt;&lt;&lt; instructions not including "leave" and "ret":</p>\n\n<pre><code>.globl swap_xor\n    .type   swap_xor, @function\nswap_xor:\n    pushl   %ebp\n    movl    %esp, %ebp\n    movl    8(%ebp), %ecx\n    movl    12(%ebp), %edx\n    movl    (%ecx), %eax\n    xorl    (%edx), %eax\n    movl    %eax, (%ecx)\n    xorl    (%edx), %eax\n    xorl    %eax, (%ecx)\n    movl    %eax, (%edx)\n    popl    %ebp\n    ret\n    .size   swap_xor, .-swap_xor\n    .p2align 4,,15\n</code></pre>\n\n<p>Summary of assembly output:<br>\nswap_traditional() takes 11 instructions<br>\nswap_xor() takes 11 instructions</p>\n\n<p>Conclusion:<br>\nBoth methods use the same amount of instructions to execute and therefore are approximately the same speed on this hardware platform.</p>\n\n<p>Lesson learned:<br>\nWhen you have small code snippets, looking at the asm output is helpful to rapidly iterate your code and come up with the fastest ( i.e. least instructions ) code.  And you can save time even because you don\'t have to run the program for each code change. You only need to run the code change at the end with a profiler to show that your code changes are faster.  </p>\n\n<p>I use this method a lot for heavy DSP code that needs speed.</p>\n'}, {'answer_id': 671298, 'author': 'jheriko', 'author_id': 17604, 'author_profile': 'https://Stackoverflow.com/users/17604', 'pm_score': -1, 'selected': False, 'text': '<p>If your compiler supports inline assembler and your target is 32-bit x86 then the XCHG instruction is probably the best way to do this... if you really do care that much about performance.</p>\n\n<p>Here is a method which works with MSVC++:</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n\n#define exchange(a,b)   __asm mov eax, a \\\n                        __asm xchg eax, b \\\n                        __asm mov a, eax               \n\nint main(int arg, char** argv)\n{\n    int a = 1, b = 2;\n    printf("%d %d --&gt; ", a, b);\n    exchange(a,b)\n    printf("%d %d\\r\\n", a, b);\n    return 0;\n}\n</code></pre>\n'}, {'answer_id': 1013116, 'author': 'Theofanis Pantelides', 'author_id': 70317, 'author_profile': 'https://Stackoverflow.com/users/70317', 'pm_score': -1, 'selected': False, 'text': '<pre><code>void swap(int* a, int* b)\n{\n    *a = (*b - *a) + (*b = *a);\n}\n</code></pre>\n\n<p>// My C is a little rusty, so I hope I got the * right :)</p>\n'}, {'answer_id': 1533223, 'author': 'Vadakkumpadath', 'author_id': 182216, 'author_profile': 'https://Stackoverflow.com/users/182216', 'pm_score': -1, 'selected': False, 'text': '<p>Another beautiful way. </p>\n\n<pre><code>#define Swap( a, b ) (a)^=(b)^=(a)^=(b)\n</code></pre>\n\n<p><strong>Advantage</strong></p>\n\n<p>No need of function call and handy.</p>\n\n<p><strong>Drawback:</strong></p>\n\n<p>This fails when both inputs are same variable. It can be used only on integer variables.</p>\n'}, {'answer_id': 15006580, 'author': 'SugarD', 'author_id': 2092613, 'author_profile': 'https://Stackoverflow.com/users/2092613', 'pm_score': 3, 'selected': False, 'text': '<p>Never understood the hate for macros. When used properly they can make code more compact and readable. I believe most programmers know macros should be used with care, what is important is making it clear that a particular call is a macro and not a function call (all caps). If <code>SWAP(a++, b++);</code> is a consistent source of problems, perhaps programming is not for you.</p>\n\n<p>Admittedly, the xor trick is neat the first 5000 times you see it, but all it really does is save one temporary at the expense of reliability. Looking at the assembly generated above it saves a register but creates dependencies. Also I would not recommend xchg since it has an implied lock prefix.</p>\n\n<p>Eventually we all come to the same place, after countless hours wasted on unproductive optimization and debugging caused by our most clever code - Keep it simple.</p>\n\n<pre><code>#define SWAP(type, a, b) \\\n    do { type t=(a);(a)=(b);(b)=t; } while (0)\n\nvoid swap(size_t esize, void* a, void* b)\n{\n    char* x = (char*) a;\n    char* y = (char*) b;\n    char* z = x + esize;\n\n    for ( ; x &lt; z; x++, y++ )\n        SWAP(char, *x, *y);\n}\n</code></pre>\n'}, {'answer_id': 21131338, 'author': 'herohuyongtao', 'author_id': 2589776, 'author_profile': 'https://Stackoverflow.com/users/2589776', 'pm_score': 3, 'selected': False, 'text': '<p><strong>For modern CPU architectures, method 1 will be faster, also with higher readability than method 2.</strong></p>\n\n<p>On modern CPU architectures, the XOR technique is considerably slower than using a temporary variable to do swapping. One reason is that modern CPUs strive to execute instructions in parallel via instruction pipelines. In the XOR technique, the inputs to each operation depend on the results of the previous operation, so they must be executed in strictly sequential order. If efficiency is of tremendous concern, it is advised to test the speeds of both the XOR technique and temporary variable swapping on the target architecture. Check out <a href="http://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice" rel="noreferrer">here</a> for more info.</p>\n\n<hr>\n\n<p><strong>Edit:</strong> Method 2 is a way of <strong>in-place swapping</strong> (i.e. without using extra variables). To make this question complete, I will add another in-place swapping by using <code>+/-</code>. </p>\n\n<pre><code>void swap(int* a, int* b)\n{\n    if (a != b) // important to handle a/b share the same reference\n    {\n        *a = *a+*b;\n        *b = *a-*b;\n        *a = *a-*b;\n    }\n}\n</code></pre>\n'}, {'answer_id': 33602527, 'author': 'Ashwin Balaji Kuppuraj', 'author_id': 4477632, 'author_profile': 'https://Stackoverflow.com/users/4477632', 'pm_score': -1, 'selected': False, 'text': "<p>Below piece of code will do the same. This snippet is optimized way of programming as it doesn't use any 3rd variable.</p>\n\n<pre><code>  x = x ^ y;\n  y = x ^ y;\n  x = x ^ y;\n</code></pre>\n"}, {'answer_id': 45844392, 'author': 'Marcin Snieg', 'author_id': 6093812, 'author_profile': 'https://Stackoverflow.com/users/6093812', 'pm_score': 2, 'selected': False, 'text': '<p>x=x+y-(y=x);</p>\n\n<pre><code>float x; cout &lt;&lt; "X:"; cin &gt;&gt; x;\nfloat y; cout &lt;&lt; "Y:" ; cin &gt;&gt; y;\n\ncout &lt;&lt; "---------------------" &lt;&lt; endl;\ncout &lt;&lt; "X=" &lt;&lt; x &lt;&lt; ", Y=" &lt;&lt; y &lt;&lt; endl;\nx=x+y-(y=x);\ncout &lt;&lt; "X=" &lt;&lt; x &lt;&lt; ", Y=" &lt;&lt; y &lt;&lt; endl;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/36915', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3798/']