On Tue, Feb 18, 2014 at 8:15 PM, Eric Davies <iam...@gmail.com> wrote:
> FYI: > > julia> function test_index() > a = rand(100,100); > pos = [1,1]; > s = 0.0 > for i = 1:10000 > s += a[pos[1], pos[2]] > end > end > test_index (generic function with 1 method) > > julia> function test_tuple() > a = rand(100,100); > pos = (1,1); > s = 0.0 > for i = 1:10000 > s += a[pos...] > end > end > test_tuple (generic function with 1 method) > > julia> test_splat(); test_normal(); test_index(); test_tuple(); > > julia> @time test_splat() > elapsed time: 0.004122959 seconds (2406892 bytes allocated) > > julia> @time test_normal() > elapsed time: 3.1479e-5 seconds (80128 bytes allocated) > > julia> @time test_index() > elapsed time: 4.3435e-5 seconds (80192 bytes allocated) > > julia> @time test_tuple() > elapsed time: 3.5078e-5 seconds (80128 bytes allocated) > > > So if you know the number of dimensions you're indexing, you can gain a > lot. Alternatively, if you have the option of using a tuple you can save a > lot there too. This may have to do with the fact that the type of the tuple > describes its length. > > > On Tuesday, 18 February 2014 12:31:52 UTC-6, Aditya Mahajan wrote: >> >> On Mon, 17 Feb 2014, Stefan Karpinski wrote: >> >> > r[pos...] will do it. >> >> This is considerable slower than normal array access. >> >> ~~~ >> function test_splat() >> a = rand(100,100); >> pos = [1,1]; >> >> # Just a silly test >> s = 0.0 >> for i = 1:10000 >> s += a[pos...] >> end >> end >> >> function test_normal() >> a = rand(100,100); >> # Just a silly test >> s = 0.0 >> for i = 1:10000 >> s += a[1,1] >> end >> end >> >> # Run both functions once >> test_splat() >> test_normal() >> >> @time test_splat() >> @time test_normal() >> ~~~~ >> >> >> Gives >> elapsed time: 0.006395408 seconds (2406892 bytes allocated) >> elapsed time: 1.5434e-5 seconds (80128 bytes allocated) >> >> Aditya >> >> Many thanks, very interesting. I was indeed worried about the relative times. Best, David.