?: Operator Vs. If Statement Performance

后端 未结 7 811
轻奢々
轻奢々 2020-12-05 23:18

I\'ve been trying to optimize my code to make it a little more concise and readable and was hoping I wasn\'t causing poorer performance from doing it. I think my changes mi

相关标签:
7条回答
  • 2020-12-05 23:30

    I suspect there won't be any performance difference.

    Next to that, I wonder why you would have any concerns of favoring one statement over the other in this case ? I mean: the performance impact (if there should be any), would be minimal. IMHO, this would be a kind of micro-optimization, and it shouldn't be worth the effort.
    I would choose the statement that is most readable, most clear, and not worry about performance since it would be of minimal influence (in this case).

    0 讨论(0)
  • 2020-12-05 23:37

    IMHO, optimize for readability and understanding - any run-time performance gains will likely be minimal compared to the time it takes you in the real-world when you come back to this code in a couple months and try to understand what the heck you were doing in the first place.

    0 讨论(0)
  • 2020-12-05 23:43

    For discussion sake... if/then/else runs just as fast as the ?: ternary operation as fast as a single level switch/case statement.

    Here are some performance benchmarks with the C# code.

    It's only when you start getting 2-3 levels deep in case statements that performance starts to be severely impacted. That is, something like this ridiculous example:

    switch (x % 3)
        {
            case 0:
                switch (y % 3)
                {
                    case 0: total += 3;
                        break;
                    case 1: total += 2;
                        break;
                    case 2: total += 1;
                        break;
                    default: total += 0;
                        break;
                }
                break;
            case 1:
                switch (y % 3)
                {
                    case 0: total += 3;
                        break;
                    case 1: total += 2;
                        break;
                    case 2: total += 1;
                        break;
                    default: total += 0;
                        break;
                }
                break;
        case 2:
                switch (y % 3)
                {
                    case 0: total += 3;
                        break;
                    case 1: total += 2;
                        break;
                    case 2: total += 1;
                        break;
                    default: total += 0;
                        break;
                }
                break;
        default:
            switch (y % 3)
            {
                case 0: total += 3;
                    break;
                case 1: total += 2;
                    break;
                case 2: total += 1;
                    break;
                default: total += 0;
                    break;
            }
            break;
        }
    
    0 讨论(0)
  • 2020-12-05 23:43

    Its a Question of whether machine level code is efficient or Human Readable Code. As we making it more Readable to us, it makes machine to do Complex interpret the code and vice versa...

    0 讨论(0)
  • 2020-12-05 23:49

    I think my changes might have slowed down my application, but it might just be in my head.

    Unless you are actually measuring performance, it's all in your head and idle speculation.

    (Not to pick on you in particular, but it is so disappointing to see question after question about performance micro-optimizations (as well as many of the answers) that do not contain the word "measure".)

    0 讨论(0)
  • 2020-12-05 23:53

    Almost no significant performance difference in this case.

    When the performance difference is negligible, it is all about readable code.

    0 讨论(0)
提交回复
热议问题