Skip to content

Commit

Permalink
using List instead of Iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhargav committed Dec 3, 2023
1 parent 1b7adcd commit e1baf61
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
18 changes: 9 additions & 9 deletions benchmark/base_conversion_bench.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ASCIIConverterBenchmark extends CustomBenchmarkBase {

@override
void run() {
// 1604 ns/op
// 1335 ns/op
b(s);
}

Expand All @@ -57,7 +57,7 @@ class NonASCIIConverterBenchmark extends CustomBenchmarkBase {

@override
void run() {
// 2007 ns/op
// 1739 ns/op
b(s);
}

Expand All @@ -79,7 +79,7 @@ class IntegerLengthFactorConverterBenchmark1 extends CustomBenchmarkBase {

@override
void run() {
// 5858 ns/op
// 4625 ns/op
b(s);
}

Expand All @@ -101,7 +101,7 @@ class IntegerLengthFactorConverterBenchmark2 extends CustomBenchmarkBase {

@override
void run() {
// 1900 ns/op
// 1538 ns/op
b(s);
}

Expand All @@ -123,7 +123,7 @@ class IntegerLengthFactorConverterBenchmark3 extends CustomBenchmarkBase {

@override
void run() {
// 1430 ns/op
// 1176 ns/op
b(s);
}

Expand All @@ -145,7 +145,7 @@ class NonIntegerLengthFactorConverterBenchmark1 extends CustomBenchmarkBase {

@override
void run() {
// 6370 ns/op
// 5090 ns/op
b(s);
}

Expand All @@ -167,7 +167,7 @@ class NonIntegerLengthFactorConverterBenchmark2 extends CustomBenchmarkBase {

@override
void run() {
// 5749 ns/op
// 4538 ns/op
b(s);
}

Expand All @@ -189,7 +189,7 @@ class NonIntegerLengthFactorConverterBenchmark3 extends CustomBenchmarkBase {

@override
void run() {
// 1436 ns/op
// 1165 ns/op
b(s);
}

Expand All @@ -211,7 +211,7 @@ class NonIntegerLengthFactorConverterBenchmark4 extends CustomBenchmarkBase {

@override
void run() {
// 1435 ns/op
// 1171 ns/op
b(s);
}

Expand Down
19 changes: 14 additions & 5 deletions lib/src/base_conversion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ class BaseConversion extends InvertibleFunction<String, String> {
final int fromBase = _fromAlphabet.radix;
final int toBase = _toAlphabet.radix;

(String, int) changeBase(Iterable<int> _values) {
(String, int) changeBase(List<int> _values) {
// Horner's method
final Iterable<int> values = _values.skipWhile((int v) => v == 0);
final List<int> values = <int>[];
for (int i = 0; i < _values.length; i++) {
if (_values[i] != 0) {
values.addAll(_values.sublist(i));
break;
}
}

if (values.isEmpty) {
return ('', 0);
Expand All @@ -86,10 +92,13 @@ class BaseConversion extends InvertibleFunction<String, String> {
return (r + _toAlphabet._characters[remainder], rLen + 1);
}

final int ipLen = ip.characters.length;
final List<int> values = <int>[];
for (final String char in ip.characters) {
values.add(_fromAlphabet._characters.indexOf(char));
}
final int ipLen = values.length;

var (String r, int rLen) =
changeBase(ip.characters.map(_fromAlphabet._characters.indexOf));
var (String r, int rLen) = changeBase(values);

if (_zeroPadding) {
final int currentLength = rLen;
Expand Down

0 comments on commit e1baf61

Please sign in to comment.