Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realize serious performance problems #109

Open
feiquan666 opened this issue Aug 16, 2024 · 1 comment
Open

Realize serious performance problems #109

feiquan666 opened this issue Aug 16, 2024 · 1 comment

Comments

@feiquan666
Copy link

It is recommended that you use big_decimal.

It is hoped that the author can read and deeply understand the implementation of Java's large number of calculations and use DART languages. Or abandon the update of this warehouse, so as not to cause unnecessary trouble for users who do not know the specific situation.

decimal Test

```dart
import 'package:decimal/decimal.dart';
import 'dart:core';

void main() {
  test(1);
  test(10);
  test(100);
  test(1000);
  test(10000);
}

void test(int loopTimes) {
  var t0 = DateTime.now();
  Decimal a = Decimal.parse('123456789.987654321');
  Decimal b = Decimal.parse('987654321.123456789');
  Decimal result = Decimal.parse("1");
  for (int i = 0; i < loopTimes; i++) {
    result = a * b * result;
  }
  var t1 = DateTime.now();
  print('Loop $loopTimes times,Cost: ${t1.difference(t0).inMilliseconds} ms');
}

```

Loop 1 times,Cost: 5 ms
Loop 10 times,Cost: 11 ms
Loop 100 times,Cost: 29 ms
Loop 1000 times,Cost: 159250 ms
Loop 10000 times,Cost: It took too long to test it

big_decimal Test

import 'package:big_decimal/big_decimal.dart';
import 'dart:core';

void main() {
  test(1);
  test(10);
  test(100);
  test(1000);
  test(10000);
  test(100000);
}

void test(int loopTimes) {
  var t0 = DateTime.now();
  BigDecimal a = BigDecimal.parse('123456789.987654321');
  BigDecimal b = BigDecimal.parse('987654321.123456789');
  BigDecimal result = BigDecimal.parse("1");
  for (int i = 0; i < loopTimes; i++) {
    result = a * b * result;
  }
  var t1 = DateTime.now();
  print('Loop $loopTimes times,Cost: ${t1.difference(t0).inMilliseconds} ms');
}

Loop 1 times,Cost: 2 ms
Loop 10 times,Cost: 0 ms
Loop 100 times,Cost: 0 ms
Loop 1000 times,Cost: 7 ms
Loop 10000 times,Cost: 363 ms
Loop 100000 times,Cost: 42112 ms

Using Java BigDecimal

package cn.javaunion.practice.calcul;

import java.math.BigDecimal;

public class BigDecimalTest {

	public static void main(String[] args) {
		test(1);
		test(10);
		test(100);
		test(1000);
		test(10000);
		test(100000);
		test(1000000);
	}

	public static void test(int loopTimes) {
		var t0 = System.currentTimeMillis();
		BigDecimal a = new BigDecimal("123456789.987654321");
		BigDecimal b = new BigDecimal("987654321.123456789");
		BigDecimal result = new BigDecimal("1");
		for (int i = 0; i < loopTimes; i++) {
			result = a .multiply(b).multiply(result);
		}
		var t1 = System.currentTimeMillis();
		System.out.printf("Loop %s times,Cost: %s ms\n", loopTimes, (t1 - t0));
	}

}

Loop 1 times,Cost: 1 ms
Loop 10 times,Cost: 1 ms
Loop 100 times,Cost: 2 ms
Loop 1000 times,Cost: 20 ms
Loop 10000 times,Cost: 249 ms
Loop 100000 times,Cost: 17558 ms

@asinel
Copy link

asinel commented Sep 16, 2024

I ran the test and I confirm that there is an issue:
decimal:

Loop 1 times,Cost: 5 ms
Loop 10 times,Cost: 2 ms
Loop 100 times,Cost: 74 ms
Loop 1000 times,Cost: 41150 ms

big_decimal:

Loop 1 times,Cost: 0 ms
Loop 10 times,Cost: 0 ms
Loop 100 times,Cost: 0 ms
Loop 1000 times,Cost: 4 ms
Loop 10000 times,Cost: 440 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants