Skip to content

Release mode is way more slower than debug mode for user defined functions. #19677

@nabinbhandari

Description

@nabinbhandari

I was trying to benchmark the performance of flutter and compare it with that of Java and C. So I chose a simple function to calculate nth prime number.

The problem is that the time taken to calculate the 20000th prime number in release mode (821 ms) was almost 8 times slower than that in debug mode (106 ms).

The method that I used is given below:

  int nThPrimeNumber(int n) {
    int counter = 0;

    for (var i = 1;; i++) {
      if (isPrime(i)) counter++;

      if (counter == n) {
        return i;
      }
    }
  }

  bool isPrime(var n) {
    if (n < 2) return false;

    for (var i = 2; i * i <= n; i++) {
      if (n % i == 0) return false;
    }

    return true;
  }

Full source code and benchmark result is available in this repository.

I know this is not a big issue and there are some serious issues which needs to be resolved. But I am creating this issue because I found that in debug mode, Flutter code was running faster than C (but only in debug mode).

If this issue is fixed for release mode, some CPU intensive tasks such as image processing could be easily (and quickly too!) performed in Dart without having the need to write them in C/C++.

Metadata

Metadata

Assignees

Labels

c: performanceRelates to speed or footprint issues (see "perf:" labels)dependency: dartDart team may need to help us

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions