A simple java functions timer;
- Motivation
- Latest news
- What is time-me?
- Core features
- Project setup after cloning
- Example
- Contributors
- License
Below code snippet in all the code base I worked on made me write this library:
long startTime = System.currentTimeMillis();
object.doExpensiveOperation();
System.out.println("Time taken :: " + (System.currentTimeMillis() - startTime) + " millis");
- Recent Releases:
- Timing futures has been released here
- Keep an eye on this, loads of features coming soon :
- Timing in nano-seconds
and many more...
- This small library helps time a java function.
- Checking timing for functions where there are no frameworks involved inspired to write this library.
- This library also allows not just time
voidfunctions but also functions that returnresultand also retains result after execution.
- Lightweight library and easy to learn API
- Timing
voidfunctions - Timing
resultoriented functions - Timing in milliseconds
- Console printer timer with/without results
- Non-console printer timer with/without results
- Build
$ ./gradlew build
- Run tests
$ ./gradlew test
- Generate java docs
$ ./gradlew javadoc
Open java doc in browser from location :
<PATH_TO_PROJECT>/build/docs/javadoc/index.html
Below class[DataFetcher] function[fetchDataIn(long time)] needs to be tested for how much time it took to fetch:
public class DataFetcher {
public List<String> fetchDataIn(long time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
return Arrays.asList("Hi", "Hello");
}
public void greet() {
System.out.println("Hello!");
}
}Timing the above function fetchDataIn(..) will look as below:
List<String> someDataFetched = ConsoleTimer.timeMe(() -> new DataFetcher().fetchDataIn(1000));The output on console will look as below:
Timed result :: TimedResult{result=[Hi, Hello], timeTakenInMillis=1003 millis}
The same can be used on void functions as below:
ConsoleTimer.timeMe(() -> new DataFetcher().greet());To time a Future:
ExecutorService service = Executors.newSingleThreadExecutor();
Future<String> future = ConsoleTimer.timeMe(service.submit(() -> new DataFetcher().greet())));
future.get();Below class[DataFetcher] function[fetchDataIn(long time)] needs to be tested for how much time it took to fetch:
public class DataFetcher {
public List<String> fetchDataIn(long time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
return Arrays.asList("Hi", "Hello");
}
public void greet() {
System.out.println("Hello!");
}
}Timing the above function fetchDataIn(..) will look as below:
TimedResult<List<String>> timedResult = ResultTimer.timeMe(() -> new DataFetcher().fetchDataIn(1000));
System.out.println("The function took :: "+timedResult.getTimeTakenInMillis()+ " millis")
System.out.println("The function result :: "+timedResult.getResult().toString())The output on console will look as below:
The function took :: 1003 millis
The function result :: [Hi, Hello]
The same can be used on void functions as below:
TimedResult result = ResultTimer.timeMe(() -> new DataFetcher().greet());
System.out.println("The function took :: "+timedResult.getTimeTakenInMillis()+ " millis")The output on console will look as below:```
The function took :: 3 millisA Future can be timed as:
ExecutorService service = Executors.newSingleThreadExecutor();
Future<TimedResult<String>> future = ResultTimer.timeMe(service.submit(() -> new DataFetcher().greet())));
TimedResult<String> result = future.get();MIT License
Copyright (c) 2019 Rahul Baphana
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.