Using 'AsParallel()' / 'Parallel.ForEach()' guidelines?

前端 未结 4 1860
说谎
说谎 2021-01-30 20:52

Looking for a little advice on leveraging AsParallel() or Parallel.ForEach() to speed this up.

See the method I\'ve got (simplified/bastardized

4条回答
  •  长发绾君心
    2021-01-30 21:14

    I would prefer to use another data structure like a Set for each alias and then use Set union to merge them.

    Something like this

    public string[] ExpandAliases(string[] countries){
        // Alias definitions
        var apac = new HashSet { "US", "FR", ...};
        ... 
    
        var aliases = new HashMap> { {"APAC": apac}, ... };
    
        var expanded = new HashSet
        foreach(var country in countries){
            if(aliases.Contains(country)
                expanded.Union(aliases[country]);
            else{
                expanded.Add(country);
        }
    
        return expanded.ToArray();
    }
    

    Note: code should be viewed as pseudo-code.

提交回复
热议问题