C# If Else Internals

Поділитися
Вставка
  • Опубліковано 8 вер 2024

КОМЕНТАРІ • 8

  • @LevelUppp
    @LevelUppp  3 роки тому +1

    How often do you use a throw helper over a simple in-place throw?

  • @leonp809
    @leonp809 3 роки тому +2

    Great video! I really like to see such things, cause like nobody knows this, but it can be useful in different scenarios.
    There is just one question I couldn't answer myself
    Let's say we have a string and want to do a null check on it.
    Would it be better for branch prediction to do the null check like
    if (c == null) return;
    Or would it better to write the hot path inside of the if-statement, so...
    if (c != null) ...
    I just checked on sharplab and variant 2 seems better, but I would like to get some opinion by an expert :)

    • @LevelUppp
      @LevelUppp  3 роки тому +1

      That depends on a lots of things but you normal code flow should not jump and since the JIT compiler treats branches as taken by default (unless you have to throw exceptions) the second code would be better.

    • @leonp809
      @leonp809 3 роки тому

      @@LevelUppp Thanks for the fast response, hope to see more of this series in the future :D

  • @AmeliusDex
    @AmeliusDex 3 роки тому +1

    So. If I'm understanding this correctly then you should aim to have the path that is expected to be taken within the if statement, and then the "fail" path outside?
    Yikes, that's basically the reverse of what I usually do!

    • @LevelUppp
      @LevelUppp  3 роки тому +3

      Only when two branch targets are returns. If one branch target is a return it will be assumed not taken.

    • @AmeliusDex
      @AmeliusDex 3 роки тому +3

      @@LevelUppp Thanks for that. It still has some impact in some areas of my code so at least I know something I can change now.
      Thanks for making these sorts of videos, this information is difficult to find elsewhere.

  • @HardRezSerhat
    @HardRezSerhat 2 роки тому

    Hi, can you share the test methods you used? Or are you using a framework? I also want to test the duration of some things, how can I do that?