Partage
  • Partager sur Facebook
  • Partager sur Twitter

Cas d'usage de stackalloc en C# ?

    6 mars 2019 à 15:38:29

    Salut,

    Simple curiosité, je me posais la question des cas où utiliser le mot-clé stackalloc avait un intérêt, en connaissez-vous ?
    Y gagne-t-on vraiment en perfs ?

    Merci d'avance !

    • Partager sur Facebook
    • Partager sur Twitter
      6 mars 2019 à 16:32:48

      Plus que des problèmes de performance, je pense à des problématique d'interopérabilité avec du code natif.
      • Partager sur Facebook
      • Partager sur Twitter
      Je recherche un CDI/CDD/mission freelance comme Architecte Logiciel/ Expert Technique sur technologies Microsoft.
        6 mars 2019 à 16:34:11

        "The sole reason to use stackalloc is performance (either for computations or interop). By using stackalloc instead of a heap allocated array, you create less GC pressure (the GC needs to run less), you don't need to pin the arrays down, it's faster to allocate than a heap array, an it is automatically freed on method exit (heap allocated arrays are only deallocated when GC runs). Also by using stackalloc instead of a native allocator (like malloc or the .Net equivalent) you also gain speed and automatic deallocation on scope exit.

        Performance wise, if you use stackalloc you greatly increase the chance of cache hits on the CPU due to the locality of data."

        d'après https://stackoverflow.com/questions/785226/practical-use-of-stackalloc-keyword

        • Partager sur Facebook
        • Partager sur Twitter

        Cas d'usage de stackalloc en C# ?

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown