diff --git a/CHANGELOG.md b/CHANGELOG.md index fda8eb21..0421f38e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 13.12.3 +- `Vector.fromList`: + - factory-constructor speed-up + ## 13.12.2 - `README`: - Add link to `solve` method api docs diff --git a/benchmark/vector/float32/vector_initializing/float32x4_from_list.dart b/benchmark/vector/float32/vector_initializing/float32x4_from_list.dart index 98b7d9d6..57a712ef 100644 --- a/benchmark/vector/float32/vector_initializing/float32x4_from_list.dart +++ b/benchmark/vector/float32/vector_initializing/float32x4_from_list.dart @@ -1,4 +1,4 @@ -// Approx. 1.1 second (MacBook Pro 2019), Dart version: 2.16.0 +// Approx. 0.62 second (MacBook Pro 2019), Dart version: 2.16.0 // Approx. 3.7 second (MacBook Air mid 2017), Dart VM version: 2.16.0 import 'package:benchmark_harness/benchmark_harness.dart'; diff --git a/benchmark/vector/float32/vector_operations/float32x4_vector_pow_float.dart b/benchmark/vector/float32/vector_operations/float32x4_vector_pow_float.dart new file mode 100644 index 00000000..a484adae --- /dev/null +++ b/benchmark/vector/float32/vector_operations/float32x4_vector_pow_float.dart @@ -0,0 +1,39 @@ +// Approx. 1.5 second (MacBook Pro 2019), Dart version: 3.0.0 + +import 'package:benchmark_harness/benchmark_harness.dart'; +import 'package:ml_linalg/dtype.dart'; +import 'package:ml_linalg/vector.dart'; + +const amountOfElements = 10000000; + +class Float32x4VectorFloatPowBenchmark extends BenchmarkBase { + Float32x4VectorFloatPowBenchmark() + : super( + 'Vector `pow` method, floating-point power; $amountOfElements elements'); + + late Vector vector; + + static void main() { + Float32x4VectorFloatPowBenchmark().report(); + } + + @override + void run() { + vector.pow(1234); + } + + @override + void setup() { + vector = Vector.randomFilled( + amountOfElements, + seed: 1, + min: -1000, + max: 1000, + dtype: DType.float32, + ); + } +} + +void main() { + Float32x4VectorFloatPowBenchmark.main(); +} diff --git a/lib/src/vector/float32x4_vector.dart b/lib/src/vector/float32x4_vector.dart index 7d460a95..4cd26874 100644 --- a/lib/src/vector/float32x4_vector.dart +++ b/lib/src/vector/float32x4_vector.dart @@ -31,12 +31,13 @@ class Float32x4Vector with IterableMixin implements Vector { Float32x4Vector.fromList(List source, this._cache, this._simdHelper) : length = source.length { _numOfBuckets = _getNumOfBuckets(source.length, _bucketSize); - _buffer = ByteData(_numOfBuckets * _bytesPerSimdElement).buffer; - final asTypedList = _buffer.asFloat32List(); + final typedList = Float32List(_numOfBuckets * _bucketSize); + + _buffer = typedList.buffer; for (var i = 0; i < length; i++) { - asTypedList[i] = source[i].toDouble(); + typedList[i] = source[i].toDouble(); } } diff --git a/lib/src/vector/float64x2_vector.g.dart b/lib/src/vector/float64x2_vector.g.dart index a523f1d2..0aebc49f 100644 --- a/lib/src/vector/float64x2_vector.g.dart +++ b/lib/src/vector/float64x2_vector.g.dart @@ -34,12 +34,13 @@ class Float64x2Vector with IterableMixin implements Vector { Float64x2Vector.fromList(List source, this._cache, this._simdHelper) : length = source.length { _numOfBuckets = _getNumOfBuckets(source.length, _bucketSize); - _buffer = ByteData(_numOfBuckets * _bytesPerSimdElement).buffer; - final asTypedList = _buffer.asFloat64List(); + final typedList = Float64List(_numOfBuckets * _bucketSize); + + _buffer = typedList.buffer; for (var i = 0; i < length; i++) { - asTypedList[i] = source[i].toDouble(); + typedList[i] = source[i].toDouble(); } } diff --git a/pubspec.yaml b/pubspec.yaml index 5f1e0c92..5c63533f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: ml_linalg description: SIMD-based linear algebra and statistics, efficient manipulation with numeric data -version: 13.12.2 +version: 13.12.3 homepage: https://github.com/gyrdym/ml_linalg environment: diff --git a/test/vector/constructors/from_list_constructor/vector_from_list_constructor_test_group_factory.dart b/test/vector/constructors/from_list_constructor/vector_from_list_constructor_test_group_factory.dart index f42df26b..07250cf5 100644 --- a/test/vector/constructors/from_list_constructor/vector_from_list_constructor_test_group_factory.dart +++ b/test/vector/constructors/from_list_constructor/vector_from_list_constructor_test_group_factory.dart @@ -56,5 +56,29 @@ void vectorFromListConstructorTestGroupFactory(DType dtype) => expect(vector.length, 0); expect(vector.dtype, dtype); }); + + test( + 'should provide correct size of vectors when summing up, length is 3', + () { + final vector1 = Vector.fromList([1, 2, 3], dtype: dtype); + final vector2 = Vector.fromList([3, 4, 5], dtype: dtype); + final vector = vector1 + vector2; + + expect(vector, equals([4, 6, 8])); + expect(vector.length, 3); + expect(vector.dtype, dtype); + }); + + test( + 'should provide correct size of vectors when summing up, length is 5', + () { + final vector1 = Vector.fromList([1, 2, 3, 4, 5], dtype: dtype); + final vector2 = Vector.fromList([3, 4, 5, 6, 7], dtype: dtype); + final vector = vector1 + vector2; + + expect(vector, equals([4, 6, 8, 10, 12])); + expect(vector.length, 5); + expect(vector.dtype, dtype); + }); }); });