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. Other Discussions
  3. The Insider News
  4. Should I avoid LINQ for performance reasons?

Should I avoid LINQ for performance reasons?

Scheduled Pinned Locked Moved The Insider News
csharplinqperformancequestion
3 Posts 3 Posters 0 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.
  • K Offline
    K Offline
    Kent Sharkey
    wrote on last edited by
    #1

    Sound Code[^]:

    Sometimes developers hear that "LINQ is slower than using a for loop" and wonder whether that means they should avoid using LINQ for performance reasons.

    Or just the other reasons

    I guess that should be reasons().other().or()?

    M 1 Reply Last reply
    0
    • K Kent Sharkey

      Sound Code[^]:

      Sometimes developers hear that "LINQ is slower than using a for loop" and wonder whether that means they should avoid using LINQ for performance reasons.

      Or just the other reasons

      I guess that should be reasons().other().or()?

      M Offline
      M Offline
      maze3
      wrote on last edited by
      #2

      im not gonna write a test to check this but 1. is there a difference of example linq, and writing 1 select. Having tried to write this, Im only 20% sure now if can be written close to the following☹️

      Enumerable.Range(1, N).AsParallel()
      .Select(n => {
      var a = new {n * 2);
      var b = Math.Sin((2 * Math.PI * a) / 1000))
      var c = Math.Pow(b, 2) } )
      .Sum();

      2. write Linq for readability instead the "depending on what for" performance.

      Richard DeemingR 1 Reply Last reply
      0
      • M maze3

        im not gonna write a test to check this but 1. is there a difference of example linq, and writing 1 select. Having tried to write this, Im only 20% sure now if can be written close to the following☹️

        Enumerable.Range(1, N).AsParallel()
        .Select(n => {
        var a = new {n * 2);
        var b = Math.Sin((2 * Math.PI * a) / 1000))
        var c = Math.Pow(b, 2) } )
        .Sum();

        2. write Linq for readability instead the "depending on what for" performance.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        To write:

        Enumerable.Range(1, N).AsParallel()
        .Select(n => n * 2)
        .Select(n => Math.Sin((2 * Math.PI * n) / 1000))
        .Select(n => Math.Pow(n, 2))
        .Sum();

        with a single Select would be more like:

        Enumerable.Range(1, N).AsParallel()
        .Select(n => Math.Pow(Math.Sin((4 * Math.PI * n) / 1000), 2) // 2 × PI × (n × 2) === 4 × PI × n
        .Sum();

        However, it shouldn't make a huge difference. LINQ already optimizes .Select(x => fn1(x)).Select(x => fn2(x)) to .Select(x => fn2(fn1(x))), so the only extra overhead would be from calling multiple delegates rather than just one.


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

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

        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