Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.10 KB | None | 0 0
  1. import 'dart:math';
  2. import 'dart:js';
  3.  
  4. void main() {
  5.   List<String> stringList1 = new List<String>();
  6.   List<String> stringList2;
  7.  
  8.   Random rnd = new Random();
  9.   for(int i = 0; i < 100000; ++i) {
  10.     stringList1.add(rnd.nextInt(1000000000).toString());
  11.   }
  12.   stringList2 = new List<String>.from(stringList1);
  13.  
  14.   // First test - uses JS implementation of Quicksort
  15.   Stopwatch sw = new Stopwatch()..start();
  16.   stringList1.sort((String a, String b) => a.compareTo(b));  
  17.   sw.stop();
  18.   print("Quicksort sorting is done in: ${sw.elapsed.inMilliseconds}");
  19.  
  20.   // Second test - let's make sure that Array.prototype.sort() is called
  21.   sw
  22.       ..reset()
  23.       ..start();
  24.   JsArray arr = new JsArray.from(stringList2);
  25.   arr.sort((String a, String b){ a.compareTo(b); });
  26.   List<String> sortedList = arr.toList();
  27.   sw.stop();
  28.   print("JS sorting is done in: ${sw.elapsed.inMilliseconds}");
  29. }
  30.  
  31. /*
  32. Results:
  33. Chrome 38: 89/155, 144/156, 100/168
  34. FX 33: 225/354, 228/349, 225/369
  35. IE 11: 1028/1435, 1028/1415, 1111/1422
  36.  
  37. Interesting: Array.prototype.sort seems to be slower for this case
  38. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement