|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
As a developer (mostly Spring MVC & JavaFX) and casual starcraft player, I would recommend everyone to just use stackoverflow for questions about programming or serverfault for questions about administration, or one of the countless question and answers pages - hell there's even a Unity3D answers site.
If you often google for problems or stacktraces you most likely already know that site but if you dont, you should start doing so.
At work I never go to these pages directly, I just use google, but still there is always a browser tab with stackoverflow open. And I don't even post questions there because I am too lazy to register but still it has helped me countless times.
|
On August 07 2013 03:03 WindWolf wrote:Show nested quote +On August 07 2013 00:27 stormchaser wrote:On August 07 2013 00:11 WindWolf wrote:On August 06 2013 23:38 spinesheath wrote:On August 06 2013 21:34 WindWolf wrote: Visual C++ 2012 question, where should I put my .ini-files so that files in the project can read it? I tried different things, and it doesn't seem to work in the same way as it did in VS2010 VS12 should put your executable into /bin/Debug/, and if you put your files in there you can access them easily using a relative path. If you put the file into your project folders and set "copy to output directory" in the file properties to "copy always" or "copy if newer", then the file should be copied into your /bin/Debug/ on build, using the relative path from the project root. I can't find the "copy to output directory" settings anywhere in VS2012. Where is it locaed, I've searched both my project settings, solution settings and file settings for my ini-file Select all of the project files in the solution explorer and in the properties panel click Copy to Output. http://i.imgur.com/YjuZzPx.pngThis is everything I see in the properties panel (The full path is whited out by me) Heh, turns out that doesn't exist for c++, I assumed it would be the same as in c# (why wouldn't it). Check out this instead: http://stackoverflow.com/questions/1568314/copy-to-output-directory-in-vc
|
On August 07 2013 03:03 WindWolf wrote:Show nested quote +On August 07 2013 00:27 stormchaser wrote:On August 07 2013 00:11 WindWolf wrote:On August 06 2013 23:38 spinesheath wrote:On August 06 2013 21:34 WindWolf wrote: Visual C++ 2012 question, where should I put my .ini-files so that files in the project can read it? I tried different things, and it doesn't seem to work in the same way as it did in VS2010 VS12 should put your executable into /bin/Debug/, and if you put your files in there you can access them easily using a relative path. If you put the file into your project folders and set "copy to output directory" in the file properties to "copy always" or "copy if newer", then the file should be copied into your /bin/Debug/ on build, using the relative path from the project root. I can't find the "copy to output directory" settings anywhere in VS2012. Where is it locaed, I've searched both my project settings, solution settings and file settings for my ini-file Select all of the project files in the solution explorer and in the properties panel click Copy to Output. http://i.imgur.com/YjuZzPx.pngThis is everything I see in the properties panel (The full path is whited out by me) EDIT: ah I see that you are using c++.
|
On August 06 2013 14:01 RoyGBiv_13 wrote:Show nested quote +On August 05 2013 18:03 gedatsu wrote:On August 05 2013 16:49 teamamerica wrote:But in Java the simplest way I can think of (and this only works for switching two integers) is b = a + b a = b - a b = b - a
You can avoid overflow by using xor: b = a^b; a = a^b; b = a^b; I think this (and your method) can create a pipeline stall however, so it's not necessarily faster than swapping with the normal method of using an extra variable. So I have a few questions: 1) In Java, is there a simple way to switch two objects without creating a temp object? You only create objects by using the 'new' command. Since you can have two names referencing the same object, that is the best way to swap them and no extra object is created. Compilers can figure out the fastest way to swap based on your architecture and language. They recognize this sort of pattern pretty well. EDIT: confirmed: xor swaps, arithemetic swaps, an temporary variable swaps are all treated by the compiler to generate identical assembly. There is one case where this is not true, when a temporary variable is used for several uses, it is assumed the user did want to use a temporary holding state, usually a register. It's true for this case and for a lot of other cases. Modern compilers are very intelligent and can make improvements on nearly all code written by a person. But I still think it's a good learning exercise to ignore this and imagine compilers as a 1-to-1 translation tool.
|
On August 07 2013 20:02 gedatsu wrote:Show nested quote +On August 06 2013 14:01 RoyGBiv_13 wrote:On August 05 2013 18:03 gedatsu wrote:On August 05 2013 16:49 teamamerica wrote:But in Java the simplest way I can think of (and this only works for switching two integers) is b = a + b a = b - a b = b - a
You can avoid overflow by using xor: b = a^b; a = a^b; b = a^b; I think this (and your method) can create a pipeline stall however, so it's not necessarily faster than swapping with the normal method of using an extra variable. So I have a few questions: 1) In Java, is there a simple way to switch two objects without creating a temp object? You only create objects by using the 'new' command. Since you can have two names referencing the same object, that is the best way to swap them and no extra object is created. Compilers can figure out the fastest way to swap based on your architecture and language. They recognize this sort of pattern pretty well. EDIT: confirmed: xor swaps, arithemetic swaps, an temporary variable swaps are all treated by the compiler to generate identical assembly. There is one case where this is not true, when a temporary variable is used for several uses, it is assumed the user did want to use a temporary holding state, usually a register. It's true for this case and for a lot of other cases. Modern compilers are very intelligent and can make improvements on nearly all code written by a person. But I still think it's a good learning exercise to ignore this and imagine compilers as a 1-to-1 translation tool.
Microoptimization is the root of all evil.
I think it's bad to do this, especially when you are learning. Do it simple first and when there is a performance bottleneck, optimize. Yes, knowing such "tricks" is nice but having simple code is even nicer.
|
On August 07 2013 04:07 spinesheath wrote:Show nested quote +On August 07 2013 03:03 WindWolf wrote:On August 07 2013 00:27 stormchaser wrote:On August 07 2013 00:11 WindWolf wrote:On August 06 2013 23:38 spinesheath wrote:On August 06 2013 21:34 WindWolf wrote: Visual C++ 2012 question, where should I put my .ini-files so that files in the project can read it? I tried different things, and it doesn't seem to work in the same way as it did in VS2010 VS12 should put your executable into /bin/Debug/, and if you put your files in there you can access them easily using a relative path. If you put the file into your project folders and set "copy to output directory" in the file properties to "copy always" or "copy if newer", then the file should be copied into your /bin/Debug/ on build, using the relative path from the project root. I can't find the "copy to output directory" settings anywhere in VS2012. Where is it locaed, I've searched both my project settings, solution settings and file settings for my ini-file Select all of the project files in the solution explorer and in the properties panel click Copy to Output. http://i.imgur.com/YjuZzPx.pngThis is everything I see in the properties panel (The full path is whited out by me) Heh, turns out that doesn't exist for c++, I assumed it would be the same as in c# (why wouldn't it). Check out this instead: http://stackoverflow.com/questions/1568314/copy-to-output-directory-in-vc Nice, thanks for the link
Edit: The command I should write was copy "$(ProjectDir)SKYENGINE_CONFIG.ini" "$(TargetDir)"
|
I've noticed you discuss xor swap. Could you please explain the idea behind it?
b = a^b; a = a^b; b = a^b;
It seems like a^b is everywhere, and I'm not sure how it is supposed to do anything or differentiate.
I know what XOR usually means, it's "exclusive or", so either A or B but not both. I still don't get it though.
|
|
|
Right, thanks. So it seems like xor swap is useless for general use from what I've read on SO and on that link.
|
On August 07 2013 23:53 darkness wrote: I've noticed you discuss xor swap. Could you please explain the idea behind it?
b = a^b; a = a^b; b = a^b;
It seems like a^b is everywhere, and I'm not sure how it is supposed to do anything or differentiate.
I know what XOR usually means, it's "exclusive or", so either A or B but not both. I still don't get it though.
A simple example (Numbers starting with b are the binary representation):
a =13 = b1101 b = 7 = b0111
b = 7^13 = b1010 = 12 a = 7^12 = b1101 = 13 b = 13^12 = b0111 = 7
In the first step you XOR both numbers together. In the second the new XOR pulls it apart and gives the opposite number, i.e. (a^b)^b = a. The third XOR then takes the combined number again and pulls out the second number, i.e. (a^b)^a = b.
|
On August 07 2013 20:52 Morfildur wrote: Microoptimization is the root of all evil. Since you brought it up - there's a thing that has been bugging me for a while: Somewhere on TL, quite a while ago, someone brought up the task of implementing
int Difference(char* number1, char* number2); I think it was part of a job interview. Now there is this trick where you subtract the characters, abusing the way ASCII sorts numbers. This seems like the intended solution for the task.
However, if I were to code this function for real, I would do it like this:
return Parse(number1) - Parse(number2); Along with a note to the guy that requested the function that he should try to clean up his mess and parse those strings much earlier. After all it's completely ridiculous to pass around strings as numbers but at the same time try to optimize that code.
So if I ever were to encounter such a question in a job interview or such, should I assume that they want to see if I know these cute tricks, or is it a trick question and the correct solution is what I would normally do?
|
|
|
On August 08 2013 05:14 spinesheath wrote:Show nested quote +On August 07 2013 20:52 Morfildur wrote: Microoptimization is the root of all evil. Since you brought it up - there's a thing that has been bugging me for a while: Somewhere on TL, quite a while ago, someone brought up the task of implementing int Difference(char* number1, char* number2); I think it was part of a job interview. Now there is this trick where you subtract the characters, abusing the way ASCII sorts numbers. This seems like the intended solution for the task. However, if I were to code this function for real, I would do it like this: return Parse(number1) - Parse(number2); Along with a note to the guy that requested the function that he should try to clean up his mess and parse those strings much earlier. After all it's completely ridiculous to pass around strings as numbers but at the same time try to optimize that code. So if I ever were to encounter such a question in a job interview or such, should I assume that they want to see if I know these cute tricks, or is it a trick question and the correct solution is what I would normally do?
I'm not sure about your specific example, but in general, when it comes to using tricks in interviews, I'd at least mention both: describe how you might make a clean, simple, maintainable implementation and then mention the cute trick. Be sure to say that you typically write the former for real work and then ask them what they want to see.
|
Funny thing about the XOR swap, in some C++ profiling I did a while ago a simple swap function in C++:
template <typename T> void Swap( T& a, T& b ) { T c = a; a = b; b = T; }
Was actually significantly more efficient, even with integers, for some reason. The above code must have produced results the compiler knew how to optimize away. The above is also simpler and more flexible than the silly XOR trick.
|
On August 08 2013 06:57 CecilSunkure wrote:Funny thing about the XOR swap, in some C++ profiling I did a while ago a simple swap function in C++: template <typename T> void Swap( T& a, T& b ) { T c = a; a = b; b = T; } Was actually significantly more efficient, even with integers, for some reason. The above code must have produced results the compiler knew how to optimize away. The above is also simpler and more flexible than the silly XOR trick.
protip, inline this function... or it could be that the compiler already did so, and that was why it was so fast...
|
On August 08 2013 07:37 RoyGBiv_13 wrote:Show nested quote +On August 08 2013 06:57 CecilSunkure wrote:Funny thing about the XOR swap, in some C++ profiling I did a while ago a simple swap function in C++: template <typename T> void Swap( T& a, T& b ) { T c = a; a = b; b = T; } Was actually significantly more efficient, even with integers, for some reason. The above code must have produced results the compiler knew how to optimize away. The above is also simpler and more flexible than the silly XOR trick. protip, inline this function... or it could be that the compiler already did so, and that was why it was so fast... Well in the test we wrote the XOR inline, and then changed it out for this function. Both had no jump.
|
On August 08 2013 07:50 CecilSunkure wrote:Show nested quote +On August 08 2013 07:37 RoyGBiv_13 wrote:On August 08 2013 06:57 CecilSunkure wrote:Funny thing about the XOR swap, in some C++ profiling I did a while ago a simple swap function in C++: template <typename T> void Swap( T& a, T& b ) { T c = a; a = b; b = T; } Was actually significantly more efficient, even with integers, for some reason. The above code must have produced results the compiler knew how to optimize away. The above is also simpler and more flexible than the silly XOR trick. protip, inline this function... or it could be that the compiler already did so, and that was why it was so fast... Well in the test we wrote the XOR inline, and then changed it out for this function. Both had no jump.
Yea with optimizations on, this function will certainly be inlined by any reasonable compiler. The wikipedia article details why the naive swap performs better than the xor version in practice. It's not necessarily because the compiler will insert the xor-swap when appropriate as it turns out xor'ing doesn't "play nice" with the rest of the system:
(1) Xor'ing of pointers or references ruins aliasing analysis which is fundamental for a variety of optimizations (e.g., constant propagation), especially in the context of C++. (2) Xor'ing operations sometimes perform horribly in practice because of CPU pipelining.
|
On August 08 2013 06:57 CecilSunkure wrote:Funny thing about the XOR swap, in some C++ profiling I did a while ago a simple swap function in C++: template <typename T> void Swap( T& a, T& b ) { T c = a; a = b; b = T; } Was actually significantly more efficient, even with integers, for some reason. The above code must have produced results the compiler knew how to optimize away. The above is also simpler and more flexible than the silly XOR trick.
What is T c = a; exactly? From my limited knowledge on generics (C++ templates), 'T' can represent any primitive data type such as int, then what is 'c' here? My guess is 'T' here is a class of primitive data type (say 'int' like when you declare array "new int"), then you make an instance called 'c'. Is this the case?
Edit: I don't know C++, I have experience with Java though. I may be completely wrong.
|
|
|
On August 08 2013 09:58 Nesserev wrote:Show nested quote +On August 08 2013 09:47 darkness wrote:On August 08 2013 06:57 CecilSunkure wrote:+ Show Spoiler +Funny thing about the XOR swap, in some C++ profiling I did a while ago a simple swap function in C++: template <typename T> void Swap( T& a, T& b ) { T c = a; a = b; b = T; } Was actually significantly more efficient, even with integers, for some reason. The above code must have produced results the compiler knew how to optimize away. The above is also simpler and more flexible than the silly XOR trick. What is T c = a; exactly? From my limited knowledge on generics, 'T' can represent any primitive data type such as int, then what is 'c' here? My guess is 'T' here is a class of primitive data type (say 'int' like when you declare array "new int"), then you make an instance called 'c'. Is this the case? Edit: I don't know C++, I have experience with Java though. I may be completely wrong. c is an object of any class T; T doesn't have to be a primitive data type. But I don't get why the 6th line is: b = T; and not b = c; its just a typo, im assuming the original question was more on the theory than actual code, therefor this is not being used as code.
And just as he says, 'T' (a template) can represent any data structure.
|
|
|
|
|
|