Skip to content

Conversation

rahuldeepattri
Copy link
Contributor

@rahuldeepattri rahuldeepattri commented Feb 25, 2021

Fix for #90

The benchmark done using JMH show performance improvement.

	@Benchmark
	public String benchmarkStringBuilder() {
		Instant now = Instant.now();
		long timestamp = now.getEpochSecond() * 1_000_000_000L + now.getNano();

		StringBuilder appendTo = new StringBuilder();
		appendTo.append(timestamp);
		return appendTo.toString();
	}

	@Benchmark
	public String benchmarkString() {
		Instant now = Instant.now();
		long timestamp = now.getEpochSecond() * 1_000_000_000L + now.getNano();

		return String.valueOf(timestamp);
	}

Result:

Benchmark                                                           Mode     Cnt   Score   Error   Units
Comparison.benchmarkString                                         thrpt     100   2.130 ± 0.052  ops/ms
Comparison.benchmarkStringBuilder                                  thrpt     100   1.801 ± 0.028  ops/ms
Comparison.benchmarkString                                          avgt     100   0.469 ± 0.011   ms/op
Comparison.benchmarkStringBuilder                                   avgt     100   0.862 ± 0.038   ms/op
Comparison.benchmarkString                                            ss     100   0.591 ± 0.095   ms/op
Comparison.benchmarkStringBuilder                                     ss     100   0.966 ± 0.118   ms/op

Since we are appending only once, String.valueOf(timestamp) will suffice.
@CLAassistant
Copy link

CLAassistant commented Feb 25, 2021

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@KarstenSchnitter KarstenSchnitter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for providing this PR and taking the time to do the benchmarks.

The indentation seems to be off, it should be 4 spaces. Did you use the formatter settings as provided in https://github.com/SAP/cf-java-logging-support/blob/master/eclipse-formatter-settings.xml?

@rahuldeepattri
Copy link
Contributor Author

Hi @KarstenSchnitter, sorry my mistake. Fixed the indentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants