jeme commented on pull request #347:
URL: https://github.com/apache/lucenenet/pull/347#issuecomment-698815793
Maybe a bit discouraging but in a simple benchmark, this doesn't seem to
have any positive impact.
``` ini
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18363.1016
(1909/November2018Update/19H2)
Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical
cores
[Host] : .NET Framework 4.8 (4.8.4200.0), X86 LegacyJIT
DefaultJob : .NET Framework 4.8 (4.8.4200.0), X86 LegacyJIT
```
| Method | Mean | Error | StdDev |
|--------------------------- |-----------:|---------:|---------:|
| CacheValue | 320.7 ns | 6.34 ns | 7.30 ns |
| NonCacheValue | 318.5 ns | 4.74 ns | 4.20 ns |
| CacheValue_PassToMethod | 6,687.3 ns | 47.91 ns | 40.01 ns |
| NonCacheValue_PassToMethod | 6,640.5 ns | 32.99 ns | 30.86 ns |
----
```csharp
public class Benchmark
{
[Benchmark]
public void CacheValue()
{
int counter = 0;
bool cached = ValueSource.Value;
for (int i = 0; i < 1000; i++)
if (cached)
counter++;
}
[Benchmark]
public void NonCacheValue()
{
int counter = 0;
for (int i = 0; i < 1000; i++)
if (ValueSource.Value)
counter++;
}
[Benchmark]
public void CacheValue_PassToMethod()
{
bool cached = ValueSource.Value;
for (int i = 0; i < 100; i++)
DooWork(cached);
}
[Benchmark]
public void NonCacheValue_PassToMethod()
{
for (int i = 0; i < 100; i++)
DooWork();
}
public void DooWork(bool workMore)
{
int counter = 0;
for (int i = 0; i < 100; i++)
if (workMore)
counter++;
}
public void DooWork()
{
int counter = 0;
for (int i = 0; i < 100; i++)
if (ValueSource.Value)
counter++;
}
}
```
Obviously benchmarks of the actual code could prove to have different
results.
Caching the boolean flag using a Lazy seems very reasonable.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]