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. how to simplify the loop?

how to simplify the loop?

Scheduled Pinned Locked Moved C#
tutorialquestionworkspace
5 Posts 5 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.
  • S Offline
    S Offline
    SRKSHOME
    wrote on last edited by
    #1

    Hi I have following loop. With one of the workflow, rangeDataMappingsToClear gets 100,000 records and rangeKeyMappings gets 10,000 records. This is taking long time hence application hangs. How to simplify this loop?

    List rangeDataMappingsToClear= new List();
    Dictionary<string, object> rangeDataMappings = new Dictionary<string, object>();
    Dictionary<string, List<string>> rangeKeyMappings = new Dictionary<string, List<string>>();

    foreach (string key in rangeDataMappingsToClear)
    {
    rangeDataMappings.Remove(key);
    foreach (List<string> formulaKeys in rangeKeyMappings.Values)
    {
    while (formulaKeys.Remove(key)) { }
    }
    }
    rangeDataMappingsToClear.Clear();

    L V P 3 Replies Last reply
    0
    • S SRKSHOME

      Hi I have following loop. With one of the workflow, rangeDataMappingsToClear gets 100,000 records and rangeKeyMappings gets 10,000 records. This is taking long time hence application hangs. How to simplify this loop?

      List rangeDataMappingsToClear= new List();
      Dictionary<string, object> rangeDataMappings = new Dictionary<string, object>();
      Dictionary<string, List<string>> rangeKeyMappings = new Dictionary<string, List<string>>();

      foreach (string key in rangeDataMappingsToClear)
      {
      rangeDataMappings.Remove(key);
      foreach (List<string> formulaKeys in rangeKeyMappings.Values)
      {
      while (formulaKeys.Remove(key)) { }
      }
      }
      rangeDataMappingsToClear.Clear();

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

      you seem to need two nested loops, nothing can remedy that, unless your problem would allow for a different data structure altogether. However, if you have 100K rangeDataMappingsToClear elements, and only 10K rangeKeyMappings elements, that would indicate rangeDataMappingsToClear holds lots of duplicates; the first step would be to simplify rangeDataMappingsToClear, eliminating the duplicates. Either make sure you don't cause duplicates (that is the cheapest solution), or create a new collection holding all the distinct elements just once; a Dictionary or HashSet could simplify this step. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      R 1 Reply Last reply
      0
      • S SRKSHOME

        Hi I have following loop. With one of the workflow, rangeDataMappingsToClear gets 100,000 records and rangeKeyMappings gets 10,000 records. This is taking long time hence application hangs. How to simplify this loop?

        List rangeDataMappingsToClear= new List();
        Dictionary<string, object> rangeDataMappings = new Dictionary<string, object>();
        Dictionary<string, List<string>> rangeKeyMappings = new Dictionary<string, List<string>>();

        foreach (string key in rangeDataMappingsToClear)
        {
        rangeDataMappings.Remove(key);
        foreach (List<string> formulaKeys in rangeKeyMappings.Values)
        {
        while (formulaKeys.Remove(key)) { }
        }
        }
        rangeDataMappingsToClear.Clear();

        V Offline
        V Offline
        V 0
        wrote on last edited by
        #3

        I'm not sure, but you could perhaps create a new list from rangeKeyMappings by applying a lambda expression on it that does not take the values containing a key. Wild guess though.

        V.

        1 Reply Last reply
        0
        • L Luc Pattyn

          you seem to need two nested loops, nothing can remedy that, unless your problem would allow for a different data structure altogether. However, if you have 100K rangeDataMappingsToClear elements, and only 10K rangeKeyMappings elements, that would indicate rangeDataMappingsToClear holds lots of duplicates; the first step would be to simplify rangeDataMappingsToClear, eliminating the duplicates. Either make sure you don't cause duplicates (that is the cheapest solution), or create a new collection holding all the distinct elements just once; a Dictionary or HashSet could simplify this step. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          R Offline
          R Offline
          RobCroll
          wrote on last edited by
          #4

          As Luc has alluded to, if one of the issues is that the rangeDataMappingsToClear list does contain a lot of duplicates, a quick and dirty fix could be to use the Distinct method

          foreach (string key in rangeDataMappingsToClear.Distinct())

          "You get that on the big jobs."

          1 Reply Last reply
          0
          • S SRKSHOME

            Hi I have following loop. With one of the workflow, rangeDataMappingsToClear gets 100,000 records and rangeKeyMappings gets 10,000 records. This is taking long time hence application hangs. How to simplify this loop?

            List rangeDataMappingsToClear= new List();
            Dictionary<string, object> rangeDataMappings = new Dictionary<string, object>();
            Dictionary<string, List<string>> rangeKeyMappings = new Dictionary<string, List<string>>();

            foreach (string key in rangeDataMappingsToClear)
            {
            rangeDataMappings.Remove(key);
            foreach (List<string> formulaKeys in rangeKeyMappings.Values)
            {
            while (formulaKeys.Remove(key)) { }
            }
            }
            rangeDataMappingsToClear.Clear();

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Do you have to use List? Or can you use Hashset?

            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