Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10503


Ignore:
Timestamp:
02/24/14 16:22:21 (11 years ago)
Author:
gkronber
Message:

#2146: minor efficiency tweak (removed unnecessary .ToArray() call)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Random/3.3/RandomEnumerable.cs

    r10466 r10503  
    183183    private static IEnumerable<T> SampleProportional<T>(this IEnumerable<T> source, IRandom random, IEnumerable<double> weights, bool windowing, bool inverseProportional) {
    184184      var sourceArray = source.ToArray();
    185       var valueArray = PrepareProportional(sourceArray, weights, windowing, inverseProportional);
     185      var valueArray = PrepareProportional(weights, windowing, inverseProportional);
    186186      double total = valueArray.Sum();
    187187
     
    195195    }
    196196    private static IEnumerable<T> SampleProportionalWithoutRepetition<T>(this IEnumerable<T> source, IRandom random, IEnumerable<double> weights, bool windowing, bool inverseProportional) {
    197       var valueArray = PrepareProportional(source.ToArray(), weights, windowing, inverseProportional);
     197      var valueArray = PrepareProportional(weights, windowing, inverseProportional);
    198198      var list = new LinkedList<Tuple<T, double>>(source.Zip(valueArray, Tuple.Create));
    199199      double total = valueArray.Sum();
    200200
    201       while (list.Count > 0) {       
    202         var cur = list.First;       
     201      while (list.Count > 0) {
     202        var cur = list.First;
    203203        double ball = cur.Value.Item2, sum = random.NextDouble() * total; // assert: sum < total. When there is only one item remaining: sum < ball
    204204        while (ball < sum) {
     
    212212    }
    213213
    214     private static double[] PrepareProportional<T>(IList<T> sourceArray, IEnumerable<double> weights, bool windowing, bool inverseProportional) {
     214    private static double[] PrepareProportional(IEnumerable<double> weights, bool windowing, bool inverseProportional) {
    215215      double maxValue = double.MinValue, minValue = double.MaxValue;
    216       double[] valueArray = new double[sourceArray.Count];
    217 
    218       var weightsEnum = weights.GetEnumerator();
    219       for (int i = 0; i < sourceArray.Count && weightsEnum.MoveNext(); i++) {
    220         valueArray[i] = weightsEnum.Current;
     216      double[] valueArray = weights.ToArray();
     217
     218      for (int i = 0; i < valueArray.Length; i++) {
    221219        if (valueArray[i] > maxValue) maxValue = valueArray[i];
    222220        if (valueArray[i] < minValue) minValue = valueArray[i];
    223221      }
    224222      if (minValue == maxValue) {  // all values are equal
    225         for (int i = 0; i < sourceArray.Count; i++) {
     223        for (int i = 0; i < valueArray.Length; i++) {
    226224          valueArray[i] = 1.0;
    227225        }
Note: See TracChangeset for help on using the changeset viewer.