c# - .net large for loop slowing down -


i have large for-loop (30k max iterations) seems consistently slowing down:

  • the first thousand iterations take 1.34s
  • after 12k iterations, next thousand take 5.31s
  • after 23k iterations, next thousand take 6.65s
  • the last thousand iterations take 7.43s

in order gain little performance switched foreach loop for loop, , tried release configuration, can't find else in this question applies me. loop in async method

why loop slow down? can avoided?

for(int iter = 0; iter < largelist1.count; iter++) {     var cl_from = largelist1[iter];     if(largelist2.any(cl => cl.str.contains(cl_from.str)))     {         datetime dt1 = //last write time of file         datetime dt2 = //last write time of different file         if(datetime.compare(dt1, dt2) > 0)         {             try             {                 copyfile(//kernel32 copyfile file overwrite);                 globals.filex++;             }             catch(exception filexx)             {                 //error handler             }         }         else         {             globals.files++;         }     }     else     {         directory.createdirectory(//create directory, no check if exists);         try         {             copyfile(//kernel32 copyfile file not overwrite);             globals.filex++;         }         catch(exception filex)         {             // error handler         }      }     gui.updatecount(globals.filef, globals.filex, globals.files); //updates iteration on textboxes     float p = (float)100.0*((float)globals.filef + (float)globals.filex + (float)globals.files)/(float)globals.totalcount;     gui.setprogress(p); //updates progressbar } 

edit: many suggested, using hashset.contains(cl_from.str) solved problem.

the nature of these 2 items, can imagine bottleneck.

for(int iter = 0; iter < largelist1.count; iter++) {     .....     if(largelist2.any(cl => cl.str.contains(cl_from.str)))     ........... 

you checking if word large list, contained within current string.

a few reasons why slower on time:

  1. initially faster because gc doesnt run much, further in loop, gc has collect more , more often.
  2. length of string cl_from.st possibly getting larger?

some points consider:

  1. how big cl_from.str , largelist2, worth creating hash of possible values in cl_from.str , checking has lookup or possibly creating hash set of largelist2 strings, , use that, iterating on each combination of string in cl_from.str.

  2. you want improve searching algorithm, e.g. check out c# .net: fastest way check if string occurs within string. or google other string search indexing/algorithm. why not use lucene.net?

  3. use .net profiler find out bottleneck is, , spending time.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -