12

I am considering posting a question regarding a particularly slow piece of code I had (~10 minutes), looking for faster ways to solve the problem. Now before I post a question I always try and figure it out myself and in this case I have managed to reduce the time taken significantly to about 5 seconds. Ideally I'd want this time even lower, so I'm still curious about other ways to solve my problem and want to post it as a question.

Now, for posting the question I see two ways of doing it.

  • I can post my question as it was originally with my super slow code and then add my newer faster method as an answer that shows 1 way to speed it up.
  • I could post my newer solution in the question as part of my research/what have you tried so far.

Which would be the preferred way to post this question? Or is this too dependent on the specific case to make a general case either way?

10
  • 32
    If it's working code then consider Code Review.
    – Bugs
    Feb 2, 2017 at 15:18
  • 7
    I think in general (Jinx88909 is likely right about your specific question) you would post the version of the code you're actually asking for help with. People don't really need to see problems you've already solved. You might explain how you got to that point but you wouldn't need to show two sets of code.
    – BSMP
    Feb 2, 2017 at 15:21
  • 1
    @Jinx88909 That's a good shout, hadn't considered that, Although the R tag over there doesn't seem very active. Feb 2, 2017 at 15:24
  • 4
    @BSMP Selfanswering questions is encouraged as far as I know, so that's not entirely true. Feb 2, 2017 at 15:24
  • I guess I misunderstood your question because I didn't think you were talking about a self Q&A. You might consider using the self-answer tag.
    – BSMP
    Feb 2, 2017 at 15:26
  • 2
    I'd suggest a self-answered question where you show a technique to get from your slow code (question) to your fast code (answer). This may attract even more speed up suggestions. If it doesn't, consider Code Review for the latest version of your code.
    – deceze Mod
    Feb 2, 2017 at 15:26
  • 8
    The traditional problem with [performance] questions is that they don't specify how much faster the code needs to be. There is a huge difference between what you must do to get code 50% faster vs x10 faster. That is research as well, the rather important kind because if you have absolutely no idea then you never know when you're done and what answer you need to select as the solution. If you got it from 10 minutes to 5 seconds then you fixed a bug, in general there is no terribly compelling reason to tell us what bugs you fixed. Feb 2, 2017 at 15:33
  • 1
  • 1
    One key thing about performance questions is to understand what you mean by "faster". How fast is fast enough? If you've gone from 10 minutes to 5 seconds, people may think you've already done enough.
    – DavidG
    Feb 3, 2017 at 0:23
  • To add to other comments about the importance of quantifying the performance goals... When you don't have a clear objective and just feel that's it's too slow. It's likely that it's already fast enough for now. It would be great if everything ran sub-ms; but sometimes that's either impossible or just not worth the effort. Even for something that's much slower than it should be: Is it worthwhile speeding up a once-per year job from say 2 minutes to 1 second if it's going to take a week out of other more important work? Feb 5, 2017 at 6:23

1 Answer 1

7

Post a new solution and ask how to get it where you want (with precise goals like "3 seconds for list of 100 numbers").

You are expected to post a question when you hit a wall in solving your problem. Show the current state, explain the expected state (i.e. for performance question provide clear goals), show what you've tried and how it did not work, and explain the overall goal (as you may be on an "XY-problem" path).

Adding "historical" notes on how you got the problem to the current state from whatever original state is unlikely to add any value to the question. If you believe the steps you took to reach the current state (like 10 min -> 5 sec) have some novel ideas in them - consider if posting a separate Q&A (self-answered) would work.

Notes on particular question

  • Such a significant improvement usually means you picked a completely wrong algorithm to solve the problem. (E.g., a common wrong way of finding the Max value in C# is to sort and pick the first.) It may be a good idea to separate finding an algorithm from writing actual code. The original state is also unlikely to be a good SO question as there are way too many ways to get things wrong. Spending time (as you've done) is a good starting point to weed out most obvious problems.

  • Having a rough understanding of expected performance is essential when optimizing code (i.e. O-notation). Have a reasonable estimate for yourself of the overall task and the particular piece you have a question about. Provide both in the question. E.g., "I have a sequence of M items and for every item group of N around it, I need foobarring. I expect O(M * n log n) because I'm iterating items and foobarring is similar to sorting, but I get what I think is O(M * n^2.5)."

  • Know standard algorithms and demonstrate that knowledge in the question. E.g., if you need linear time for sorting you either pick non-comparison algorithms or rethink the overall solution rather than hoping to come up with a linear sort on your own.

4
  • 7
    Actually, the original (slow) code may be better to understand and speed-up using the proper technique (algorithm) than the newer, optimised code that might have taken a step in the wrong direction.
    – Bergi
    Feb 2, 2017 at 18:52
  • 2
    @Bergi while indeed it could be useful, but in this particular case of 100x improvement I'd assume original approach was completely off rather than just merely non-optimized. If original code serves as explanation of the problem better than the latest code it is fine to post both, but just showing original code for historical reasons is no value for the post in my opinion. Feb 2, 2017 at 19:08
  • 2
    I'd add that if it's feasible: a performance related question benefits a lot from providing all 'setup code' to establish a baseline. If people are expected to set up their own baseline it imposes more work on them, and adds potential for a small yet significant difference; which only clouds the Q&A. E.g. For processing a list, ensure a sample list is provided or generated. For SQL questions, provide table, and index CREATE and INSERT scripts. Feb 5, 2017 at 6:03
  • 2
    Also since it's likely to be a long question. Make your problem statement clear and concise up front. You don't want people faced with a text and code wall that's going to take 10 minutes to read and digest. Feb 5, 2017 at 6:05

You must log in to answer this question.

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