Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Performance of Switch case vs dictionary with delegates

Performance of Switch case vs dictionary with delegates

Scheduled Pinned Locked Moved C#
htmlvisual-studiocomperformancequestion
22 Posts 5 Posters 101 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard DeemingR Richard Deeming

    For literals, the other alternative would be to use a UTF8 string literal:

    ReadOnlySpan bytes = "ABCD"u8;
    int value = System.Runtime.InteropServices.MemoryMarshal.Read(bytes);
    // 0x44434241

    It's not going to compile to a single instruction, but it should be fairly well optimized.


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    T Offline
    T Offline
    trønderen
    wrote on last edited by
    #21

    That solves the issue if you restrict yourself to 7-bit ASCII. Even for West European languages (such as Norwegian), a character may fill more than one octet. So if you step through a string 4 characters at a time, converting them to 32 bits, you will miss 8 bits here and there. Can lead to nasty, hard-to-debug errors when it happens with a customer on the other side of the earth, using a language with lots of multi-octet UTF8 characters.

    Religious freedom is the freedom to say that two plus two make five.

    1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      That was added in C# 11, back in November 2022. :) UTF-8 string literals - C# feature specifications | Microsoft Learn[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      T Offline
      T Offline
      trønderen
      wrote on last edited by
      #22

      I think that is one of the (few) useful extensions to C# in recent years.

      Religious freedom is the freedom to say that two plus two make five.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups