Sooner or later, absolutely every programmer is likely to come across such a concept as measuring performance.
In any office, in any team, and even when you are alone with your Tyler Durden (but only if your Tyler is a programmer), at least once, there will be a dispute about how to implement this or that function so that it works fast. But quickly, as a characteristic, is usually not quoted, so I propose to talk about how to quickly turn an abstract into a non-abstract number.
Tools
You can measure performance with different tools, let's talk about some of those that I have come across.
Date
A native data structure describing a date / time.
All measurements boil down to the fact that we measure the date before the function, then the date after the function, and take the difference.
Needless to say, there can be no question of any redundant accuracy of such measurements due to the peculiarities of storing the date in the OS.
The system clock is initialized from the hardware when the operating system boots, and then the system time is maintained using regular timer interrupts. ( Wikipedia )
Simply put, the time is cached and updated at a certain frequency, and the accuracy of our measurements cannot exceed the frequency of this update.
, Date , , , ± 100 . Date .
Performance.now()
.
Node.js , PerformanceTiming.navigationStart.
:
const start = performance.now();
myAwesomeFunc();
const end = performance.now();
//
const diffSec = (end - start) / 1000;
// - .
console.log('op/sec: ' + (1 / diffSec);
op/sec, 0.00000546654.
Performance.now() Date, . - timestamp , .
Benchmark.js
var suite = new Benchmark.Suite;
// add tests
suite.add('RegExp#test', function() {
/o/.test('Hello World!');
})
.add('String#indexOf', function() {
'Hello World!'.indexOf('o') > -1;
})
.add('String#match', function() {
!!'Hello World!'.match(/o/);
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// running
.run();
Benchmark.js . mocha, , .
. , : js, .
:
— , .
.
function checkLen(array: number[]) {
let len = 0;
for (let i = 0; i< 1_000_000; i++) {
len = array.length;
}
return len;
}
*: 720.4278 op/sec
*- .
, .
, , , .
, , , .
, Hrodvitnir? , . -, :
function checkLen(array: number[]) {
let len = 0;
len = array.length;
for (let i = 0; i< 1_000_000; i++) {
}
return len;
}
: 718.3247 op/sec
: .
: 0.28%, , . , .
, . , js .
- . , , , . .
, .
, , . . LICM.
, :
function checkLen(
array: number[],
len: number[] // 1000000
) {
for (let i = 0; i< 1_000_000; i++) {
len[i] = array.length;
}
return len;
}
: 330.0807
, , , , , , , , , , .
, .
, , .
, .
1 1,000,000.
const testArray = _.range(1, 1_000_000).toArray();
// 1 1,000,000
function checkFilter(array: number[]) {
return _(array).where(item => !!(item % 2)).toArray()
}
: 23.4559
-, , filter , lodash.
, —
, :
: 13.3961
. , lodash, .
, - .
:
No | ||
---|---|---|
1 | 30 | 30 |
2 | 27 | 28.5 |
3 | 18 | 25 |
4 | 24 | 24.75 |
5 | 13 | 22.4 |
, , .
, 10 , 10 .
, , , .
, , "", , , , -. , , , .
: .
.
, , / , . ., , , .
. , .
, , , .
( , ).
, , , .
, "": . , .
, . , .
.
:
function checkFilter(array: number[]) {
return _(array).where(item => !!(item % 2)).toArray()
}
:
function checkFilter(array: number[]) {
_(array).where(item => !!(item % 2)).toArray()
}
, , , , , .
, , , .
, , , . , .
, - — .
10,000,000 75 .
, .
, . , .
, .
, , , 10 , 200 .
, 200 , , .
, . , , . , . , , — .
. , .
, , .
— 25% , 25% , . .
: , , .
. .
, - , , , , .
:
function checkFilter(array: number[]) {
return _(array).where(item => !!(item % 2)).toArray()
}
:
: 23.4559
: ? , . ?
, / .
Benchmarking is a very fun activity that helps to notice problem areas in the code in time and make it better. But this is also an occupation associated with its own difficulties.
In this article, I tried to answer the questions that arose in my time, tried to highlight some interesting points.
Thank you for attention!