forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
both within a method, and between two methods Used to determine whether GZip makes sense
- Loading branch information
1 parent
95e2ec5
commit 4945a96
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (c) 2023 David Allison <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.ichi2.utils | ||
|
||
import androidx.annotation.CheckResult | ||
import com.ichi2.libanki.utils.TimeManager | ||
import timber.log.Timber | ||
|
||
/** | ||
* Usage: | ||
* | ||
* ```kotlin | ||
* val result = measureTime("operation") { operation() } | ||
* ``` | ||
* -> `D/TimeUtilKt executed mHtmlGenerator in 23ms` | ||
*/ | ||
fun <T> measureTime(functionName: String? = "", function: () -> T): T { | ||
val startTime = TimeManager.time.intTimeMS() | ||
val result = function() | ||
val endTime = TimeManager.time.intTimeMS() | ||
Timber.d( | ||
"executed %sin %dms", | ||
if (functionName.isNullOrEmpty()) "" else "$functionName ", | ||
endTime - startTime | ||
) | ||
return result | ||
} | ||
|
||
/** | ||
* Used to time an operation across two function calls | ||
* | ||
* ```kotlin | ||
* private val renderStopwatch: Stopwatch = Stopwatch.init("page render") | ||
* | ||
* fun start() { | ||
* renderStopwatch.reset() | ||
* } | ||
* | ||
* fun stop() { | ||
* renderStopwatch.logElapsed() | ||
* } | ||
* ``` | ||
* | ||
* -> `D/Stopwatch executed page render in 67ms` | ||
*/ | ||
class Stopwatch(private val executionName: String?) { | ||
private var startTime = TimeManager.time.intTimeMS() | ||
|
||
fun logElapsed() { | ||
val endTime = TimeManager.time.intTimeMS() | ||
Timber.d( | ||
"executed %sin %dms", | ||
if (executionName.isNullOrEmpty()) "" else "$executionName ", | ||
endTime - startTime | ||
) | ||
} | ||
|
||
fun reset() { | ||
startTime = TimeManager.time.intTimeMS() | ||
} | ||
companion object { | ||
|
||
/** initializes the stopwatch to ensure `stop()` before `start()` won't crash */ | ||
@CheckResult | ||
fun init(executionName: String? = null) = Stopwatch(executionName) | ||
} | ||
} |