c# - No performance gains with Parallel.ForEach and Regex? -


i have program color codes returned results set way depending on results are. due length of time takes color-code results (currently being done regex , richtextbox.select + .selectioncolor), cut off color-coding @ 400 results. @ around number takes 20 seconds, max time of i'd consider reasonable.

to try improve performance re-wrote regex part use parallel.foreach loop iterate through matchcollection, time same (18-19 seconds vs 20)! not job lends parallel programming well? should try different? advice welcome. thanks!

ps: thought bit strange cpu utilization never went 14%, or without parallel.foreach.

code

matchcollection startmatches = regex.matches(temprtb.text, startpattern);  object locker = new object(); system.threading.tasks.parallel.foreach(startmatches.cast<match>(), m => {     int = 0;     foreach (group g in m.groups)     {         if (i > 0 && < 5 && g.length > 0)         {             temprtb.invoke(new func<bool>(                 delegate                 {                     lock (locker)                     {                         temprtb.select(g.index, g.length);                         if ((i & 1) == 0) // number                             temprtb.selectioncolor = namespace.properties.settings.default.valuecolor;                         else              // odd number                             temprtb.selectioncolor = namespace.properties.settings.default.attributecolor;                         return true;                     }                 }));         }         else if (i == 5 && g.length > 0)         {             var result = temprtb.invoke(new func<string>(                 delegate                 {                     lock (locker)                     {                         return temprtb.text.substring(g.index, g.length);                     }                 }));              matchcollection submatches = regex.matches((string)result, pattern);              foreach (match submatch in submatches)             {                 int j = 0;                 foreach (group subgroup in submatch.groups)                 {                     if (j > 0 && subgroup.length > 0)                     {                         temprtb.invoke(new func<bool>(                             delegate                             {                                 lock (locker)                                 {                                     temprtb.select(g.index + subgroup.index, subgroup.length);                                     if ((j & 1) == 0) // number                                         temprtb.selectioncolor = namespace.properties.settings.default.valuecolor;                                     else              // odd number                                         temprtb.selectioncolor = namespace.properties.settings.default.attributecolor;                                     return true;                                 }                             }));                     }                     j++;                 }             }         }         i++;     } }); 

virtually no aspect of program actually able run in parallel.

the generation of matches needs done sequentially. can't find second match until has found first. parallel.foreach will, @ best, allow process results of sequence in parallel, still generated sequentially. majority of time consuming work seems be, , there no gains there.

on top of that, aren't really processing results in parallel either. majority of code run in body of loop inside invoke ui thread, means it's being run single thread.

in short, tiny, tiny bit of program run in parallel, , using parallelization in general adds overhead; sounds you're barely getting more overhead. there isn't did wrong, operation inherently doesn't lend parallelization, unless there effective way of breaking initial string several smaller chucks regex can parse individually (in parallel).


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -