Created
February 15, 2021 14:12
-
-
Save tischi/17a72c142bd554c98857f7c4a4ac8668 to your computer and use it in GitHub Desktop.
Benchmark StackView
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
Code: | |
```Java | |
import de.embl.cba.bdp2.imglib2.LazyStackView; | |
import net.imglib2.RandomAccess; | |
import net.imglib2.RandomAccessibleInterval; | |
import net.imglib2.img.array.ArrayImgs; | |
import net.imglib2.type.numeric.integer.ByteType; | |
import net.imglib2.view.StackView; | |
import java.util.ArrayList; | |
public class BenchmarkLazyStackView | |
{ | |
public static void main( String[] args ) | |
{ | |
final int numSlices = 10000; | |
final int nx = 100; | |
final int ny = 100; | |
long nanoTime; | |
long duration; | |
final ArrayList< Long > oldTimes = new ArrayList<>(); | |
final ArrayList< Long > newTimes = new ArrayList<>(); | |
for ( int repetition = 0; repetition < 10; repetition++ ) | |
{ | |
// Old way | |
nanoTime = System.nanoTime(); | |
final StackView< ByteType > stackView = new StackView<>( getRandomAccessibleIntervals( numSlices, nx, ny ) ); | |
final RandomAccess< ByteType > access = stackView.randomAccess(); | |
for ( int i = 0; i < numSlices; i++ ) | |
access.setPositionAndGet( 0, 0, i ); | |
duration = System.nanoTime() - nanoTime; | |
System.out.println( "Old DefaultRA [ns]: " + duration ); | |
oldTimes.add( duration ); | |
// New way | |
nanoTime = System.nanoTime(); | |
final LazyStackView< ByteType > lazyStackView = new LazyStackView<>( getRandomAccessibleIntervals( numSlices, nx, ny ) ); | |
final RandomAccess< ByteType > lazyAccess = lazyStackView.randomAccess(); | |
for ( int i = 0; i < numSlices; i++ ) | |
lazyAccess.setPositionAndGet( 0, 0, i ); | |
duration = System.nanoTime() - nanoTime; | |
System.out.println( "New DefaultRA [ns]: " + duration ); | |
newTimes.add( duration ); | |
} | |
System.out.println( "Old average = " + oldTimes.stream().mapToDouble( x -> x ).summaryStatistics().getAverage() ); | |
System.out.println( "New average = " + newTimes.stream().mapToDouble( x -> x ).summaryStatistics().getAverage() ); | |
} | |
private static ArrayList< RandomAccessibleInterval< ByteType > > getRandomAccessibleIntervals( int numSlices, int nx, int ny ) | |
{ | |
final ArrayList< RandomAccessibleInterval< ByteType> > rais = new ArrayList<>(); | |
for ( int i = 0; i < numSlices; i++ ) | |
{ | |
rais.add( ArrayImgs.bytes( nx, ny ) ); | |
} | |
return rais; | |
} | |
} | |
``` | |
Output: | |
``` | |
Old DefaultRA [ns]: 341932322 | |
New DefaultRA [ns]: 49534316 | |
Old DefaultRA [ns]: 53715032 | |
New DefaultRA [ns]: 135685258 | |
Old DefaultRA [ns]: 27748677 | |
New DefaultRA [ns]: 210612767 | |
Old DefaultRA [ns]: 10658957 | |
New DefaultRA [ns]: 9818483 | |
Old DefaultRA [ns]: 32998927 | |
New DefaultRA [ns]: 8296665 | |
Old DefaultRA [ns]: 9182600 | |
New DefaultRA [ns]: 42703781 | |
Old DefaultRA [ns]: 78796986 | |
New DefaultRA [ns]: 7036566 | |
Old DefaultRA [ns]: 12728417 | |
New DefaultRA [ns]: 9498186 | |
Old DefaultRA [ns]: 11396330 | |
New DefaultRA [ns]: 30719648 | |
Old DefaultRA [ns]: 8561321 | |
New DefaultRA [ns]: 12746940 | |
Old average = 5.87719569E7 | |
New average = 5.1665261E7 | |
``` | |
Conclusion: This looks like as if there is no performance issue doing it the new way, for the tested access pattern. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@tpietzsch
I tried here to benchmark the LazyStackView. This seems to indicate that there is no performance issue.
Please let me know if you'd like me to test other access patterns.