What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 134 Stack Exchange communities.

Speeding up the if statment by eliminating else

The poster made a trivial mistake and almost everyone answering gleefully pointed it out instead of fixing it and discussing the actual question. Yes, maybe it's a silly one but that's not the reason to gang up on the poster. Even the accepted "answer" consists of the compiler output with errors which has nothing to do with what's asked.

If I'm an outsider this would give me an impression that SO users is a bunch of jaded self-important "experts" who won't deign to answer unless you ask something obscure that tickles their brain cells.

Also, "closed as not a real question"? How is it not a question?

EDIT the question has been deleted so here's the cached copy from my RSS:


In terms of assembly or machine code, is there any difference between these two piece of code? Note: a is user's input. First:

int a = 10;
bool b = false;

if (a > 5)
    b = true;     // just changing the value

Second:

int a = 10;

if (a > 5)
    bool b = true;    // declaration placed here ...
else
    bool b = false;    

I'm thinking of Eliminating the Else part, in terms of micro-optimisation. Regardless of how much it will speed up the code, my question is, is there any difference between above codes in the compiled binary or not?


The mistake was putting the declaration of b inside the if and else, but it's obvious IMO what he's actually asking. And before you accuse him of not checking if it compiles, have you never done the same thing? Edited the code before posting to emphasize something and did not double check because "it was a minor change, surely it's still valid code"?


EDIT2 Question has been undeleted, thanks!

share|improve this question
3  
I'm not entirely sure what behavior you are referring to. The OP posted a bad question, and it was closed by the community. Would you prefer those types of terrible, localized questions being upvoted to the top of the list by the community? –  Richard J. Ross III Mar 22 '13 at 12:52
1  
"Even the accepted "answer" consists of the compiler output with errors which has nothing to do with what's asked." ... well, given that there is nothing to answer (the code is wrong after all) what kind of an answer would you expect? And if you're worried about people getting rep out of the answer, then I'd say that the question is likely to be deleted if the OP doesn't improve it. –  Bart Mar 22 '13 at 12:53
2  
All aboard the deletion train! Choo Choo –  Richard J. Ross III Mar 22 '13 at 12:54
2  
At SO, I have a strong impression, that questions very rarely are fixable. It's no use even trying unless you're very quick about it. As soon as downvotes start to pile up, it's a lost cause, and at that point the best one can hope for is to get a comment through to the OP, that they should delete this question and ask again, with a list of things they should fix. Rest is up to the OP, if they learn the lesson or not. –  hyde Mar 22 '13 at 13:20
3  
related: Close all the typo questions –  gnat Mar 22 '13 at 13:33
1  
@Bart The OP didn't get a chance to improve it - it was closed within in hour, a deleted within two. –  Hannele Mar 22 '13 at 13:58
2  
@Hannele So the OP had 2 full hours to improve it. –  Bart Mar 22 '13 at 13:58
8  
Was it necessary to delete it this quickly though? Now it's become virtually impossible to discuss it here. –  Pëkka Mar 22 '13 at 14:03
    
@Pekka웃 It wasn't deleted by a mod, so you can vote to undelete. –  Gilles Mar 22 '13 at 14:21
3  
@Bart Two full hours? How luxurious! Some people have a life, you know. Two days is the minimum. –  Gilles Mar 22 '13 at 14:22
1  
@IgorSkochinsky 1. Get 10k reputation. 2. Browse the page (you need to have kept the link). 3. Click the undelete button. It only works if the delete votes were cast by community members, you can't vote to undelete a moderator-deleted post. For moderator-deleted posts, you have to flag, and the SO mods unfortunately don't consider a question being discussed on meta to be grounds for undeletion so it's chancy at best. –  Gilles Mar 22 '13 at 14:26
2  
@Gilles Whether it should have been deleted or not is debatable. Point is that the user posted a problematic question. He received a significant number of comments indicating that. He subsequently accepted an answer pointing out the problematic nature of his question and did absolutely nothing to improve the question. This was by no means a user who left because he had other things to do in the mean time. In this case having it sit around for 2 more days seems rather pointless. –  Bart Mar 22 '13 at 14:31
1  
perhaps "should I object to community members answering the question that was asked, instead of what the poster meant?" It has more detail than "this behaviour" and it's an actual question about what you should do when you see this happening. Or "why was this question mis-answered and closed, then deleted quickly?" if you just want to understand the situation –  Kate Gregory Mar 22 '13 at 14:42
1  
I hope it's better now. –  Igor Skochinsky Mar 22 '13 at 14:49
1  
Nice! Good edit. –  Kate Gregory Mar 22 '13 at 14:51

3 Answers 3

I disagree.

The OP couldn't even be bothered to compile their code to see if there might be a problem. They then posted a question asking whether incorrect code was the same as something else?

This is not a real question. The answer is "no", and the OP could have found this out easily for themselves. Instead, they waste the time of everyone who answers the question. More importantly, they dilute the search results for anyone trying to find their own answer.

I agree that the community doesn't always react in the best way to stupid mistakes; but, when asking a question, a basic amount of research is required first. Stack Overflow gets thousands of questions a day; maintaining the quality standards is difficult (impossible). Something does, sometimes, give way. I don't even think that the comments, in this case, are particularly bad. They politely, though a little sarcastically, point out where the OP is incorrect.

share|improve this answer
3  
Wow, nice job deleting it so quickly. In fact, the answer to the actual question ("is there difference on machine code level") is "Yes". I was in process of editing it to make it less trivial and hopefully reopen to provide an answer but now it's gone. –  Igor Skochinsky Mar 22 '13 at 13:10
3  
I didn't delete the question @Igor, I don't have enough reputation to do so (it requires 20k if it was closed recently); I probably wouldn't have deleted it anyway. There are far worse questions out there that deserve immediate deletion: deleted by BЈовић, Richard J. Ross III, juergen d 11 mins ago –  ben is uǝq backwards Mar 22 '13 at 13:18
3  
@Igor - The answer is "Yes" because the code does different things. That makes it an even worse question than the usual micro optimizations - "Is this a nanosecond faster? Oops does different things!". –  Bo Persson Mar 22 '13 at 13:23
    
@benisuǝqbackwards: thanks and sorry for my outburst at you. I don't fully agree with you but thanks for being polite and not snarky like some others. –  Igor Skochinsky Mar 22 '13 at 14:12

I agree with you.

You are saying that the question was really whether the following code

bool b;

if (a > 5)
    b = true;    // declaration placed here ...
else
    b = false;    

with a being some kind of variable, is equivalent, in terms of generated code, to this:

bool b = false;

if (a > 5)
    b = true;     // just changing the value

And that's a fair question. The OP would probably benefit from hearing about optimization flags, constant folding, dead code elimination, and most to the point, liveness analysis.

The aspect whether a is a constant or a genuine variable would be worth discussing itself.

Some consequences could be drawn for code readability and against the cargo cult of micro-optimizations.

That's all true, but I also agree with the other side. The original poster did a bad job and posted a bad question, that would make any good answer MUCH longer and full of explanations of side issues (e.g., duplicate declarations), that would obscure the whole post. It is even possible not to understand which bit of their invalid code they are asking about.

So I think that it was equally fair to edit, answer, or delete, and you weren't the Fastest Gun in the West - which your or my type of people usually aren't.

Deleting a question does not prevent anyone from posting a much improved version of the same question as needed.

share|improve this answer
    
"The OP would probably benefit from hearing about optimization flags, constant folding, dead code elimination, and most to the point, liveness analysis." Why would the OP be hearing anything about that when the answer is a simple matter of running it through the compiler? Not to mention the fact that it's compiler-dependent, and he didn't mention which one. Which means that no answer can be correct. –  Nicol Bolas Mar 22 '13 at 15:55
2  
@NicolBolas - It is not a simple matter of running it through the compiler, because it is compiler dependent. You need best practices that are reasonably safe against future technological progress that your code is likely to interact with. And to understand and explain motivation of those best practices, you need a basic understanding of compiler technology state-of-the-art. –  Jirka Hanika Mar 22 '13 at 15:58
    
Then you're agreeing that the question, as stated, is effectively unanswerable. If the only possible answer is some arbitrary "best practices" based on what people think a bunch of compilers might do in the future... I don't see how that's a reasonable thing to expect to ever happen. Also, I don't recall when "best practices" questions became legal SO questions. –  Nicol Bolas Mar 22 '13 at 16:01
2  
@NicolBolas - Not at all. It is a good and answerable question when you strip away the bad syntax. If you doubt that, ask it on SO and you'll be flooded with great answers, even if you don't specify a particular compiler. It's a legal question as well. –  Jirka Hanika Mar 22 '13 at 16:05

I do think the community acted a little too rabidly in this instance - was the question really so bad that it deserved closing/deletion in a matter of hours?

It seemed like he was asking about something that Stack Overflow people should encourage (coding standards and optimization) - perhaps it was somewhat trivial, but hardly deserving such perfunctory deletion.

It also seemed like the poster was new to such questions about optimization. Could the community have been so sure that he knew where to look? (I suppose, that single answer at least showed the OP how to do that, which may have been why he accepted it.) An answer that outlines why his sample code didn't fully capture his intent would also likely have been hugely useful to anyone interested in learning more about code optimization.

share|improve this answer
2  
Reading the question it doesn't seem all that hostile. The OP asked which was faster, there were comments along the lines of, "Why don't you try running the two programs and see for yourself; if the behavior is unexpected, perhaps ask a question about that." The question showed no real research/effort, the comment (rather politely, in my eyes) suggested ways in which he could discover the answer for himself. –  Servy Mar 22 '13 at 15:09
    
@Servy Reading the now restored responses, it's possible I overreacted after no longer being able to read the question. –  Hannele Mar 22 '13 at 22:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .