-3

I was a little discouraged such a huge difference in performance when working on 32-bit machines with 32-bit integers in Dart language.

Is this means that Dart VM still not optimized for integer arithmetic?

Here is my pretty straightforward test.

void main() {
  var tests = new List();
  tests.add({"name" : "16-bit", "result" : 0});
  tests.add({"name" : "32-bit", "result" : 0});
  tests[0]["result"] = test1(0x8000);
  tests[1]["result"] = test1(0x80000000);
  int best;
  for(var test in tests) {
    var result = test["result"];
    if(best == null) {
      best = result;
    } else if(best > result) {
      best = result;
    }
  }

  for(var test in tests) {
    var result = test["result"];
    var percent = (result / best * 100).round();
    print("${test["name"]}: $percent%");
  }
}

int test1(int value) {
  int result = 0;
  var count = 10000000;
  var sw = new Stopwatch();
  sw.start();
  for(var i = 0; i < count; i++) {
    var result = value + i;
  }

  sw.stop();
  return sw.elapsedMicroseconds;
}

Output:

16-bit: 100%
32-bit: 13285%

This means that in some real cases performance can be slower in 130 times?

mezoni
  • 10,684
  • 4
  • 32
  • 54
  • Your test if flawed. `test2` cannot use 32 bit integers since the number gets to large. – ronag Feb 01 '14 at 09:39
  • 1
    I think that programmers that do not understand when and what happens when numbers exceed the limit are "wrong to programmers". – ronag Feb 01 '14 at 10:41
  • I would say to those programmers that Dart might not be the right tool for the job, if they have such VERY specific requirements. I am not sure what point you are trying to make with your ranting... – ronag Feb 01 '14 at 13:23
  • And sure, I can think of situations when one would need integers larger than `0x80000000`. However, I can't think of many situations where I would **not have access to a 64 bit machine** and **need such large values** in **performance critical code**. – ronag Feb 01 '14 at 13:25
  • 3
    Seeing your rude comments and snide edited post... I don't even know why I bother trying to help you. I'm starting to understand why you were banned from the Dart mailing list... I wish you luck with your programming... you seem to need it... – ronag Feb 01 '14 at 13:29
  • If you do not need my help then why did you post this question? I don't see anyone else feeling the need to answer it (I wonder why). I think the Dart team simply found you as self serving, insulting and annoying as the rest of us. – ronag Feb 01 '14 at 15:48
  • @ronag your words: "I'm starting to understand why you were banned from the Dart mailing list". If you will be constantly reminded Dart Team about lack of elementary concepts, I think that you also were banned from the Dart mailing list. I mean the lack of support for users as such. Total ignorance of user requests. Development method that known as "in the dark", ie almost complete lack of technical specifications of entire SDK when everything is taken "from the ceiling". And so on. All this and much more is the cause of the poor quality of the finished product. – mezoni Feb 09 '14 at 10:57

2 Answers2

4

Your test is flawed. test2 cannot use 32 bit integers since the number gets to large.

If you modify your test as follows they will take exactly the same time:

library lexer_perf;

import '_perf.dart';
import 'package:angular/core/parser/lexer.dart';

void main() {
  var tests = new List();
  tests.add({"name" : "16-bit", "result" : 0});
  tests.add({"name" : "32-bit", "result" : 0});
  tests[1]["result"] = test1(0x3fffffff); // 2^30 is the maximum small (32bit) integer value
  tests[0]["result"] = test1(0xffff);
  int best;
  for(var test in tests) {
    var result = test["result"];
    if(best == null) {
      best = result;
    } else if(best > result) {
      best = result;
    }
  }

  for(var test in tests) {
    var result = test["result"];
    var percent = (result / best * 100).round();
    print("${test["name"]}: $percent%");
  }
}

int test1(int value) {
  int result = 0;
  var count = 10000000;
  var sw = new Stopwatch();
  sw.start();
  for(var i = 0; i < count; i++) {
    var result = value - i; // Subtract instead of add so that we do not exceed maximum small integer value.
  }

  sw.stop();
  return sw.elapsedMicroseconds;
}

Output:

16-bit: 122% // Well within margin of error.
32-bit: 100%
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
ronag
  • 49,529
  • 25
  • 126
  • 221
1

Original question: "Is it possible to write in Dart the high performance code that works on 32-bit machines with 32-bit integers?"

Short Answer: No. It is possible to write high-performance code for 31bit integers, but not 32bit integers.

Why?

Dart doesn't have the notion of a 32bit integer.

Integers are, by Dart spec, arbitrary-length. But for performance reasons, there are different internal representations for smaller ranges. The problem is that the small int range is not 32bit, but 31bit (on a 32bit system). Thus, everything between [-(2^30+1), -(2^31)] or [2^30, 2^31-1] is not a small int anymore, but a medium int which is 64bit.

The full ranges, for completeness sake, are:

System          smi      mint     bigint
[32bit system]: 31bit    64bit    limited by RAM
[64bit system]: 63bit    64bit    limited by RAM

Source: https://www.dartlang.org/articles/numeric-computation/#integers

MarioP
  • 3,752
  • 1
  • 23
  • 32