33 /**
44 * JSONDB - JSON Database Manager
55 *
6- * Manage local databases with JSON files and JSON Query Language (JQL)
6+ * Manage JSON files as databases with JSON Query Language (JQL)
77 *
88 * This content is released under the MIT License (MIT)
99 *
@@ -59,7 +59,8 @@ class Benchmark {
5959 * @return void
6060 */
6161 public function mark ( $ name ) {
62- self ::$ marker [$ name ] = microtime ( ) ;
62+ self ::$ marker [$ name ]['e ' ] = microtime ( ) ;
63+ self ::$ marker [$ name ]['m ' ] = memory_get_usage ( ) ;
6364 }
6465
6566 /**
@@ -73,24 +74,41 @@ public function elapsed_time( $point1 = '', $point2 = '', $decimals = 4 ) {
7374 if ( $ point1 === '' ) {
7475 return '{elapsed_time} ' ;
7576 }
76- if ( !array_key_exists ( $ point1 , self ::$ marker )) {
77+ if ( !array_key_exists ( $ point1 , self ::$ marker ) ) {
7778 return '' ;
7879 }
79- if ( !array_key_exists ( $ point2 , self ::$ marker )) {
80- self ::$ marker [$ point2 ] = microtime ( ) ;
80+ if ( !array_key_exists ( $ point2 , self ::$ marker ) ) {
81+ self ::$ marker [$ point2 ]['e ' ] = microtime ( ) ;
82+ self ::$ marker [$ point2 ]['m ' ] = memory_get_usage ( ) ;
8183 }
82- list ( $ sm , $ ss ) = explode ( ' ' , self ::$ marker [$ point1 ] ) ;
83- list ( $ em , $ es ) = explode ( ' ' , self ::$ marker [$ point2 ] ) ;
84+ list ( $ sm , $ ss ) = explode ( ' ' , self ::$ marker [$ point1 ][ ' e ' ] ) ;
85+ list ( $ em , $ es ) = explode ( ' ' , self ::$ marker [$ point2 ][ ' e ' ] ) ;
8486
8587 return number_format ( ( $ em + $ es ) - ( $ sm + $ ss ), $ decimals ) ;
8688 }
8789
8890 /**
8991 * Calculate the memory usage of a benchmark point
90- * @return int
92+ * @param string $point1 The name of the first benchmark point
93+ * @param string $point2 The name of the second benchmark point
94+ * @param int $decimals
95+ * @return mixed
9196 */
92- public function memory_usage ( ) {
93- return memory_get_usage ();
97+ public function memory_usage ( $ point1 = '' , $ point2 = '' , $ decimals = 4 ) {
98+ if ( $ point1 === '' ) {
99+ return '{memory_usage} ' ;
100+ }
101+ if ( !array_key_exists ( $ point1 , self ::$ marker ) ) {
102+ return '' ;
103+ }
104+ if ( !array_key_exists ( $ point2 , self ::$ marker ) ) {
105+ self ::$ marker [$ point2 ]['e ' ] = microtime ( ) ;
106+ self ::$ marker [$ point2 ]['m ' ] = memory_get_usage ( ) ;
107+ }
108+ $ sm = self ::$ marker [$ point1 ]['m ' ] ;
109+ $ em = self ::$ marker [$ point2 ]['m ' ] ;
110+
111+ return number_format ( $ em - $ sm , $ decimals ) ;
94112 }
95113
96114 }
0 commit comments