Skip to content

Instantly share code, notes, and snippets.

@ayoubzulfiqar
Created August 5, 2023 08:14
Show Gist options
  • Save ayoubzulfiqar/6f11015e1040bb95cad762fbe0112f44 to your computer and use it in GitHub Desktop.
Save ayoubzulfiqar/6f11015e1040bb95cad762fbe0112f44 to your computer and use it in GitHub Desktop.
This is simple Testing and Benchmarking show case which type of class Method is Fast e.g [intance type, static type, private static type]

Method Testing

OddEven Sorting Algorithm for Testing Purpose

void oddEvenSort(List<int> arr, int n) {
  bool isSorted = false;

  while (!isSorted) {
    isSorted = true;
    int temp = 0;

    // Perform Bubble sort on odd indexed elements
    for (int i = 1; i <= n - 2; i += 2) {
      if (arr[i] > arr[i + 1]) {
        temp = arr[i];
        arr[i] = arr[i + 1];
        arr[i + 1] = temp;
        isSorted = false;
      }
    }

    // Perform Bubble sort on even indexed elements
    for (int i = 0; i <= n - 2; i += 2) {
      if (arr[i] > arr[i + 1]) {
        temp = arr[i];
        arr[i] = arr[i + 1];
        arr[i + 1] = temp;
        isSorted = false;
      }
    }
  }
}

Class - Intance based

class Iter {
  void oddEvenSort(List<int> arr, int n) {
    final Stopwatch stopwatch = Stopwatch()..start();
    bool isSorted = false;

    while (!isSorted) {
      isSorted = true;
      int temp = 0;

      // Perform Bubble sort on odd indexed elements
      for (int i = 1; i <= n - 2; i += 2) {
        if (arr[i] > arr[i + 1]) {
          temp = arr[i];
          arr[i] = arr[i + 1];
          arr[i + 1] = temp;
          isSorted = false;
        }
      }

      // Perform Bubble sort on even indexed elements
      for (int i = 0; i <= n - 2; i += 2) {
        if (arr[i] > arr[i + 1]) {
          temp = arr[i];
          arr[i] = arr[i + 1];
          arr[i + 1] = temp;
          isSorted = false;
        }
      }
    }
    stopwatch.stop();
    print('Iter Took: ${stopwatch.elapsedMicroseconds} microseconds');
  }
}

Class with static Method

class StaticIter {
  static void oddEvenSort(List<int> arr, int n) {
    final Stopwatch stopwatch = Stopwatch()..start();
    bool isSorted = false;

    while (!isSorted) {
      isSorted = true;
      int temp = 0;

      // Perform Bubble sort on odd indexed elements
      for (int i = 1; i <= n - 2; i += 2) {
        if (arr[i] > arr[i + 1]) {
          temp = arr[i];
          arr[i] = arr[i + 1];
          arr[i + 1] = temp;
          isSorted = false;
        }
      }

      // Perform Bubble sort on even indexed elements
      for (int i = 0; i <= n - 2; i += 2) {
        if (arr[i] > arr[i + 1]) {
          temp = arr[i];
          arr[i] = arr[i + 1];
          arr[i + 1] = temp;
          isSorted = false;
        }
      }
    }
    stopwatch.stop();
    print('Static Iter Took: ${stopwatch.elapsedMicroseconds} microseconds');
  }
}

Class with Private Static Methods

class PrivateStaticIter {
  static oddEvenSort(List<int> arr, int n) {
    final Stopwatch stopwatch = Stopwatch()..start();
    _oddEvenSort(arr, n);
    stopwatch.stop();
    print(
        'Private Static Iter Took: ${stopwatch.elapsedMicroseconds} microseconds');
  }

  static void _oddEvenSort(List<int> arr, int n) {
    bool isSorted = false;

    while (!isSorted) {
      isSorted = true;
      int temp = 0;

      // Perform Bubble sort on odd indexed elements
      for (int i = 1; i <= n - 2; i += 2) {
        if (arr[i] > arr[i + 1]) {
          temp = arr[i];
          arr[i] = arr[i + 1];
          arr[i + 1] = temp;
          isSorted = false;
        }
      }

      // Perform Bubble sort on even indexed elements
      for (int i = 0; i <= n - 2; i += 2) {
        if (arr[i] > arr[i + 1]) {
          temp = arr[i];
          arr[i] = arr[i + 1];
          arr[i + 1] = temp;
          isSorted = false;
        }
      }
    }
  }
}

Initialization

Calling each methods

class Algorithm {
  first(List<int> arr, int n) => Iter().oddEvenSort(arr, n);
  second(List<int> arr, int n) => StaticIter.oddEvenSort(arr, n);
  third(List<int> arr, int n) => PrivateStaticIter.oddEvenSort(arr, n);
}

Generating Array

  final List<int> array = List.generate(100000, (index) => Random().nextInt(10000));

Running Code

void main() {
  final algorithm = Algorithm();
  algorithm.first(array, array.length);
  algorithm.second(array, array.length);
  algorithm.third(array, array.length);
}

Test for Addition Type of Testing

void main() {
  final List<int> array = List.generate(1000, (index) => Random().nextInt(100));
  group('Iteration', () {
    test('IterTEST', () {
      Iter().oddEvenSort(array, array.length);
    });

    test('Static Iter', () {
      StaticIter.oddEvenSort(array, array.length);
    });
    test('Private Static Iter', () {
      PrivateStaticIter.oddEvenSort(array, array.length);
    });
  });
}

Benchmarking Results

First Run

Iter Took: 88857909 microseconds
Static Iter Took: 2789 microseconds
Private Static Iter Took: 2868 microseconds

Second Run

Iter Took: 87715375 microseconds
Static Iter Took: 2993 microseconds
Private Static Iter Took: 2886 microseconds

Third Run

Iter Took: 87956866 microseconds
Static Iter Took: 3137 microseconds
Private Static Iter Took: 2798 microseconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment