Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more
  • I was about to give you a "Best Practices" vote for using the modulo method instead of converting to a string.
    But then you had a while(true)/break, so they cancel each other out.
    Look into the do {} while(...) statement.

  • This comment is hidden because it contains spoiler information about the solution

  • Here, not only twice, but 2 * str.lenght times.

  • You are totally right. Thanks for this comment. But a lot of time has passed since I wrote this. And it was just like "hmmm, can I write it in one line in a couple of minutes?". Now I'm working as a middle .Net developer and I would never do that in my production code. My Upper solution is extremely slow.

  • This comment is hidden because it contains spoiler information about the solution

  • The regex is the completely wrong solution here. It's quite slow relative to the rest of the method, by at least one order of magnatude. Which is a high price for something which is ultimately going to fail -- the start of the pattern say "match semi-colon or anything in the range ..." which means bar, open bracket & close bracket will get through, along with any whitespace character aside from a blank (such as a tab).

  • This comment is hidden because it contains spoiler information about the solution

  • You're doing the ToLower inside the loop, so you're doing the same thing (probably the most time consuming step in the whole function) over & over 26 times.

  • Sorry, but you're wrong. I win.

  • I'm aware of the quote. However, it's not particularly relevant here.

    It mainly concerns itself with wasting lots of programmer time optimizing a minor part of the overall application.

    Now granted, I did waste a lot of time running the benchmark, but I only did that for the second post, because I wanted real numbers for the message --- Just from looking at it, I knew the original version would be more than an order of magnitude slower.

    Further, the orginal version offers no advantage (such as expandability, thread-saftey, or even readability), to justify it running slower. It just pointlessly wastes time.

    Had the original been written as:
    string GetPlanetName(int id)
    {
    Task.Sleep(20);
    return planets[id - 1];
    }
    would you say removing the Sleep call was a premature optimization?

    The entire point of this site is to train master programmers. However, the three points I made in the previous post are not about optimization or mastery, but rather basic competency. Knowing things like those (as well as appreciating the difference between things done at compile-time vs run-time) is a necessary skill of any developer.

  • This comment is hidden because it contains spoiler information about the solution

  • It's a valid solution as long as it only contains single words.
    With a seperator you're basically back to an array again, except for speed / memory usage maybe.
    Good solution in this case, but no best practice i'd guess. :)

  • you're kidding, right

  • Eeek.... Doing a Split() on every call --- just because you are too lazy to add the quotes & commas to make a proper array??

  • Loading more items...