-79

There's no way around it: this question is stupid would, if it had been posted years ago, have gotten closed as "not a real question." It shows two code snippets and asks whether the one that does less stuff is faster. There's no explanation of why the performance is important, there's no attempt at testing timing, there's no attempt at critical thinking ("does it take time to do stuff?"). Here's the original version of the question in its entirety:

Is there any difference between following two code snippets? and Which is a faster and Why?

case 1:

def func():
        a = 42
        return a

case 2:

def func():
        return 42

Anyone who can't answer this for themselves is going to ask a new question, because they won't be able to find this post.

However, it's getting upvotes and has a highly-upvoted answer.

This is downright embarrassing. Do we have any quality standards at all?

35
  • 23
    If this was C++ and I actually know what is going on I would probably DV the Q and leave a comment along the lines of Any decent compiler should create the same code. You should compile the code and check the assembly to see for yourself. To me it is a lack of research effort (did not check the assembly). Apr 13, 2017 at 11:48
  • 14
    @NathanOliver: But it is python and not C++. And in python these two code samples will not lead to the same result. I agree that the question is not optimal, but could be rephrased to a more useful: "Why do these two samples not result in the same assembly?".
    – BDL
    Apr 13, 2017 at 12:18
  • 5
    @BDL But they still could have looked up how to compare the generated code and then tried it. After doing that if they came to SO and said they had these two codes with the disassembly and wanted to know why the compiler did not optimize the first to the second then I think that would be a great question. I just feel not checking the disassembly first, themselves, is a reason to DV the Q. Apr 13, 2017 at 12:20
  • 27
    I agree this question isn't great, and this is somewhat unrelated, but do you have any clear litmus test for quality standards yourself Tigerhawk? Because taking a cursory glance at your Answers page, I expected to see what I would consider very high quality questions being answered, given that you don't hesitate to downvote & leave a canned-comment on every answer for just about everything else... but I'm having some trouble seeing the distinction.
    – miradulo
    Apr 13, 2017 at 12:23
  • 30
    For instance, recently you answered this and this, but DV/can-commented this. What is the distinction?
    – miradulo
    Apr 13, 2017 at 12:24
  • 31
    It seems a perfectly good question to me. My guess is that the compiler would produce the same code for both by optimising the first one, but, not being a SW engineer, I don't know that. And I wouldn't have a clue at how to look at assembly code: why do you think the asker of this question would know? Apr 13, 2017 at 13:21
  • 8
    I personally believe there is no "stupid" question. You are right, there's no attempt of providing extra thoughts or timing test- and THAT is what not ok about this question. But if we take that line of code, and compare the way it works in C/Ruby- the difference can teach a lot about both languages.
    – Chen
    Apr 13, 2017 at 13:25
  • 6
    I don't think you can expect a user with a rep of about 200 to analyse the assembler code generated by the compiler to check for him or herself.
    – Steve Ives
    Apr 13, 2017 at 13:32
  • 22
    @SteveIves Rep is a measure of experience with the site, not experience as a programmer.
    – Servy
    Apr 13, 2017 at 13:32
  • 19
    @Tom If that is the case (In the ideal world) then there wouldn't be any questions at all. If you, or any person on the internet can answer it, the OP can do his research and eventually find out the answer, maybe after weeks, months, years, 10 years etc. It is also a matter of finding "experts" and finding answers and solving problems in a reasonable amount of time. I'm not saying "basic research" is not important. I'm saying expecting someone to dissect bite-code and otherwise calling him "stupid" is a bit too much... Not everyone has the same expertise
    – T J
    Apr 13, 2017 at 13:36
  • 5
    @Servy True - maybe a better point is that you can't expect someone who asks a question like that to check the generated assembler code.. :-) to someone more experienced, it's obvious that before it can return 42 it'll have to store the 42 somewhere, (probably a register, maybe in storage) and will probably be a bit quicker than returning a variable, but that real-world differences will be negligible.
    – Steve Ives
    Apr 13, 2017 at 13:37
  • 6
    @NathanOliver The notion that the assembly is going to tell you anything about the answer to this question is a fundamental misunderstanding about computer programming. You could use a good answer to this question.
    – philipxy
    Apr 13, 2017 at 13:54
  • 17
    In my experience, there are 10 types of programmers: those that are helpful, and those that are condescending... Apr 13, 2017 at 13:57
  • 5
    @Tom, Servy and the other close voters: I really wonder why you closed the question although the answer and votings here clearly indicate that this is an acceptable question.
    – BDL
    Apr 13, 2017 at 15:22
  • 3
    I'm curious as to why the meta-style voting approach is playing out so broadly on the original question. Regardless of whether or not people agree with this post; surely the original post does not merit 33 upvotes and 21 downvotes, and counting. Since this post was broadly about the integrity of SO, it's disappointing that people on both sides of the fence are walking all over that with overblown voting on the OP who, note, asked for none of this.
    – roganjosh
    Apr 13, 2017 at 15:40

1 Answer 1

122

This is not a nonsense question. It's a perfectly reasonable question that I didn't know the answer to despite having been a professional Python programmer for years.

(It was admittedly badly-titled and therefore unlikely to be useful to future readers in its initial incarnation, but Josh Caswell's edit took care of that fine.)

Your objections seem to be twofold:

  1. The answer is obvious, and therefore asking the question is stupid.

    I disagree. In many languages, the two code snippets exhibited would compile down to exactly the same lower-level code. To recognise that this isn't the case in Python, one either needs to have at least a basic knowledge of the optimisation powers of Python's compiler, or needs to have the knowledge required to analyse the byte code produced by it in order to see that it is different for these two snippets. Plenty of Python programmers won't have the knowledge required to do either of these things, because they've simply never needed to. I am such a person, even though I have 500 upvotes across my Python contributions here and have worked with the language for years. You're assuming that this knowledge is basic and common to all Python programmers when it really, really isn't.

    And even if none of that were the case, basic questions are allowed here. "The stupid asker is stupid for not already knowing the answer to his question, so he shouldn't be allowed to ask it" is not one of our close reasons, and it shouldn't be.

  2. There is no point in knowing the answer to the question anyway

    This doesn't necessarily seem true. I agree that it would be a strange scenario indeed where the cost of an assignment had a performance impact that mattered (although I'm not convinced that it could literally never matter), but this is an interesting question just as a case study in the powers of Python's byte code compiler, and is useful in that capacity. Having read the answer, I understand more about Python's byte code compilation than I did before. As such, the question was useful to me.

I see no reason for this question to be downvoted or closed, at all.

21
  • 57
    This. What I wanted to say too, both as a moderator and as a python regular. If something seems obvious to you, step aside for a moment and assess your own assumptions first.
    – Martijn Pieters Mod
    Apr 13, 2017 at 14:01
  • 10
    1. The question was not, "do these compile to the same byte code". The question was which one is faster. This is answered by simply running the two snippets, something that any programmer interested in the answer is going to be capable of doing. 2. You say that it matters, and then immediately follow it up by saying that it doesn't matter. That you were entertained by it, despite it not being useful, doesn't change that.
    – Servy
    Apr 13, 2017 at 14:02
  • 5
    @Ðаn: Then the OP should provide examples that exemplify the problem. This one does not. Apr 13, 2017 at 14:04
  • 10
    @Servy no one is saying that question was perfect and did not need editing. They are saying the basis of the question aren't enough to close and that it is a valid question. In my environment, I don't always have the luxury of just be able to run what ever test code I want, so I sympathise with the asker of the question. Apr 13, 2017 at 14:05
  • 30
    @Servy: Naive performance tests in a single environment are generally not useful. Much better to have an understanding of what's actually going on. Apr 13, 2017 at 14:06
  • 5
    @SaggingRufus But it's not an appropriate question, and it's problems aren't just trivially fixed with a surface level edit. If you're not in a position of being able to run any code at all then why would you care about the answer to the question? Any time that you'd need to know the answer to the question would be a time where you are in a position to actually run the code.
    – Servy
    Apr 13, 2017 at 14:07
  • 5
    The questions wasn't presented very well at the beginning (and one of the edits did take care of that) but it was relatively clear what was asked and it definitely was answerable. Time measurements are (imho) nothing that was needed here since it would only test one configuration in one environment, while it was possible to answer the question in a very general way. Analyzing assemblies on the other hand is something I really wouldn't expect a python programmer to know. I even doubt that a lot of C++ programmers have ever looked at the disassembly.
    – BDL
    Apr 13, 2017 at 14:08
  • 4
    @T.J.Crowder If you want to actually understand which performs better, you have that the other way around. If you run the two snippets you know what the performance difference is. If you ask a stranger on the internet what they think the performance difference will be, then you've only learned what some stranger on the internet thinks the difference will be.
    – Servy
    Apr 13, 2017 at 14:10
  • 2
    @Servy That's not what I said, of course I can run code and do tests. In my position these things are planned out and we don't make change for change sake. I am also so run off my feet with actual development work that there is no way I could take time out of my day to set a testbed to test something that may or may not be implemented into my codeset. Because of this, I would ask a question to the community. Apr 13, 2017 at 14:11
  • 4
    @SaggingRufus "I couldn't be bothered to do the work myself, so I'll just ask other people to do it for me" isn't actually an appropriate reason to ask a question on SO. The help center specifically says that people are expected to be doing their research and trying to find an answer to their question before asking on SO, only asking if they're unable to solve their own problem, not asking becuase they can't be bothered to solve the problem.
    – Servy
    Apr 13, 2017 at 14:14
  • 8
    @Servy: But if you ask a community of people with a strong peer review ethos (SO) whether there's a difference between the examples (which the OP did in the first revision), that's very different from asking some random stranger or relying on your own naive test. That's asking for what's happening under-the-covers. Which is what the OP got. Apr 13, 2017 at 14:14
  • 3
    @Servy: Well, we just disagree about that. The peer review aspect is crucially different, in my view. And I think the result clearly supports my view. You clearly think otherwise. That's fine, you don't have to agree with me, nor I with you. Apr 13, 2017 at 14:19
  • 17
    @T.J.Crowder I'll leave the general case for other answerers to address, if they wish to. My personal view - about which I'm much less certain than I am about this single question - is that the community generally does a decent job of being welcoming to good content and hostile to bad content, and that the loudest voices complaining about our lack of standards are - as in this particular case - usually advocating for arbitrary or unhelpfully harsh standards that I don't agree with. So in a sense I guess my answer does address the general case, just by way of a case study.
    – Mark Amery
    Apr 13, 2017 at 14:30
  • 4
    @T.J.Crowder But fundamental to "do we encourage poor-quality questions" is "what counts as a 'poor-quality question'?" It seems to be that the community disagrees with TigerhawkT3 on what a 'poor-quality question' is. If we can't agree on what we're talking about, how can we ever come to consensus on what to do about it? -- If the example presented in this meta question doesn't encapsulate what TigerhawkT3 (or others) thinks a 'poor-quality question' is, then this is a poor-quality meta question. :-)
    – R.M.
    Apr 13, 2017 at 14:34
  • 3
    @R.M.: Indeed. Amusingly, the question might fall short of the quality standard it promotes, by using only a single example. :-) Or not, if this is a bang-on accurate example. And Mark's comment above, along the same lines, is well-taken too. If the example is emblematic of the problem described by the question, addressing the example addresses the question. Apr 13, 2017 at 16:02

You must log in to answer this question.

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