1function test91
2%TEST91 test subref performance on dense vectors
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7fprintf ('\n------------------------------ testing GB_mex_Matrix_subref\n') ;
8
9[save save_chunk] = nthreads_get ;
10chunk = 4096 ;
11nthreads = feature ('numcores') ;
12nthreads_set (nthreads, chunk) ;
13
14ntrials = 10 ;
15
16% addpath old
17rng ('default')
18
19n = 10 * 1e6 ;
20A = sparse (rand (n,1)) ;
21fprintf ('A is a sparse %d-by-1 vector, all nonzero\n', n) ;
22F = full (A) ;
23
24
25for ilen = [1 10 100 1000 10000 100000 1e6]
26
27    fprintf ('\n----- C(I) = A (I), I is random with length(I) = %d\n', ilen) ;
28
29    I  = irand (1, n, ilen, 1) ;
30    I0 = uint64 (I-1) ;
31
32    tic
33    for trials = 1:ntrials
34        C1 = A (I) ;
35    end
36    tm = toc ;
37    fprintf ('MATLAB sparse: %g sec\n', tm) ;
38
39    tic
40    for trials = 1:ntrials
41        Cfull = F (I) ;
42    end
43    tf = toc ;
44    fprintf ('MATLAB full:   %g sec\n', tf) ;
45
46    J0 = uint64 (0) ;
47
48    tic
49    for trials = 1:ntrials
50        C3 = GB_mex_Matrix_subref (A, I0, J0) ;
51    end
52    tg = toc ;
53    fprintf ('GraphBLAS:     %g sec speedup %g\n', tg, tm/tg) ;
54    assert (isequal (C1, C3)) ;
55
56end
57
58fprintf ('\n----- C(:) = A (:)\n') ;
59
60tic
61for trials = 1:ntrials
62    C1 = A (:) ;
63end
64tm = toc ;
65fprintf ('MATLAB:        %g\n', tm) ;
66
67tic
68for trials = 1:ntrials
69    C3 = GB_mex_Matrix_subref (A, [ ], J0) ;
70end
71tg = toc ;
72assert (isequal (C1, C3)) ;
73fprintf ('GraphBLAS:     %g sec speedup %g\n', tg, tm/tg) ;
74
75F = full (A) ;
76
77tic
78for trials = 1:ntrials
79    C0 = F (:) ;
80    C0 (1) = 1 ;    % make sure the copy gets done, not a lazy copy
81end
82tf = toc ;
83fprintf ('\nMATLAB (full): %g\n', tf) ;
84
85nthreads_set (save, save_chunk) ;
86