-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-measurements.php
32 lines (27 loc) · 1.17 KB
/
01-measurements.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
/*
* Measuring Memory Usage
*
* PHP includes two functions for checking memory usages without the use of
* profiling tools or testing environments. These tools do not provide a high
* level of fidelity in measurement and may be report inconsistent values; but
* it's still a great way to get a sense of the current state of your script.
*
* memory_get_usage(bool $real_usage = false): int
* Returns the amount of memory, in bytes, that's currently being allocated.
* https://www.php.net/manual/en/function.memory-get-usage.php
*
* memory_get_peak_usage(bool $real_usage = false): int
* Returns the peak of memory, in bytes, that's been allocated to your script.
* https://www.php.net/manual/en/function.memory-get-peak-usage.php
*/
// Get initial memory usage.
echo "Initial usage: " . round(memory_get_usage()/1024,2) . " kb\n";
// Allocate some memory.
$list = range(0, 100000);
echo "After creating list: " . round(memory_get_usage()/1024,2) . " kb\n";
// Deallocate memory.
unset($list);
echo "After un-setting list: " . round(memory_get_usage()/1024,2) . " kb\n";
// Report peak usage.
echo "Peak usage: " . round(memory_get_peak_usage()/1024,2) . " kb\n";