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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. i wait so much because of loops

i wait so much because of loops

Scheduled Pinned Locked Moved C#
questionalgorithmsperformance
23 Posts 6 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.
  • P Pete OHanlon

    BTW - your code sample won't compile. The following line is the problem because of an uninitialized variable:

    G[i, j] = Math.Min(G[i, j], G[i, j] + G[j, j]); // special case k==j

    "WPF has many lovers. It's a veritable porn star!" - Josh Smith

    As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

    My blog | My articles | MoXAML PowerToys | Onyx

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #21

    you're right, I replaced k's by j's, it should have been the other way around. Sorry for that. :)

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


    Prolific encyclopedia fixture proof-reader browser patron addict?
    We all depend on the beast below.


    K 1 Reply Last reply
    0
    • L Luc Pattyn

      you're right, I replaced k's by j's, it should have been the other way around. Sorry for that. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Prolific encyclopedia fixture proof-reader browser patron addict?
      We all depend on the beast below.


      K Offline
      K Offline
      karayel_kara
      wrote on last edited by
      #22

      hi ,i modified my code folowing that, but i get some error folowing code running ! double[,] a, b, c; a = new double[2000, 2000]; b=new double[2000,2000]; c=new double[2000,2000]; int s = 2000; Parallel.For(0, s, delegate(int i) { for (int j = 0; j < s; j++) { double v = 0; for (int k = 0; k < s; k++) { v += a[i, k] * b[k, j]; } c[i, j] = v; } }); bu my code isn't run :( Parallel.For(0, nrons, delegate(int k) { for (i = 0; i < nrons; i++) for (j = 0; j < nrons; j++) //G[i, j] = Math.Min(G[i, j], G[i, k] + G[k, j]); if (G[i, j]> (G[i, k] + G[k, j]) ) G[i, j] =(G[i, k] + G[k, j]) ; } ); why did i get error ?

      L 1 Reply Last reply
      0
      • K karayel_kara

        hi ,i modified my code folowing that, but i get some error folowing code running ! double[,] a, b, c; a = new double[2000, 2000]; b=new double[2000,2000]; c=new double[2000,2000]; int s = 2000; Parallel.For(0, s, delegate(int i) { for (int j = 0; j < s; j++) { double v = 0; for (int k = 0; k < s; k++) { v += a[i, k] * b[k, j]; } c[i, j] = v; } }); bu my code isn't run :( Parallel.For(0, nrons, delegate(int k) { for (i = 0; i < nrons; i++) for (j = 0; j < nrons; j++) //G[i, j] = Math.Min(G[i, j], G[i, k] + G[k, j]); if (G[i, j]> (G[i, k] + G[k, j]) ) G[i, j] =(G[i, k] + G[k, j]) ; } ); why did i get error ?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #23

        karayel_kara wrote:

        my code isn't run

        is not informative. does it compile? if not, what is the first error? does it run? if not, what is the first exception, with all the details? and at what line is it pointing? anyway, your code looks all wrong. You now have three arrays. Why? You don't initialize the arrays. I see six for loops, you only need three. And the way I understand the algorithm you can not possibly run the outer loop in parallel (that was the essence of my comment to Pete too). And please, please, please, please, please, please, show code in PRE tags, not in CODE tags. Final remark: when all you do to the algorithm is run it in parallel, the best you can hope for is to get it N times faster, with N the number of cores, say 4. That is something, but not much. And there are no guarantees. My first experiment did run 5 times slower, because it trashed the caches all the time! (and I'm afraid that will be true for your app too). :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Prolific encyclopedia fixture proof-reader browser patron addict?
        We all depend on the beast below.


        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