It seems that runtime of lapack's dgemm function
depends on the densities of its arguments
(as contrasted to maxima's ".").
So dgemm outperforms "." only when the matrices
are very sparse.
Below you see the results of squaring 400 by 400
and 900 by 900 binary matrices with different
numbers of 1's.
I use Maxima 5.22.1 in Windows XP.
squaring 400 by 400 binary matrix:
# of 1's "." runtime "dgemm" runtime
0 8.4 s 4.2 s
130 8.2 s 5.6 s
296 8.2 s 7.2 s
462 8.2 s 9.1 s
squaring 900 by 900 binary matrix:
# of 1's "." runtime "dgemm" runtime
0 94 s 21 s
824 94 s 42 s
1568 94 s 62 s
2394 94 s 86 s
Maxima session:
<setting display2d and showtime, loading "graphs" and "lapack">
(%i6) A:adjacency_matrix(random_graph(400,0.0))
Evaluation took 0.0500 seconds (0.0500 elapsed)
(%i7) mat_trace(A . A)/2
Evaluation took 8.3900 seconds (8.3900 elapsed)
(%o7) 0
(%i8) mat_trace(dgemm(A,A))/2
Evaluation took 4.1700 seconds (4.1700 elapsed)
(%o8) 0.0
(%i9) A:adjacency_matrix(random_graph(400,0.001))
Evaluation took 0.0500 seconds (0.0500 elapsed)
(%i10) mat_trace(A . A)/2
Evaluation took 8.2000 seconds (8.2000 elapsed)
(%o10) 65
(%i11) mat_trace(dgemm(A,A))/2
Evaluation took 5.6200 seconds (5.6200 elapsed)
(%o11) 65.0
(%i12) A:adjacency_matrix(random_graph(400,0.002))
Evaluation took 0.0300 seconds (0.0300 elapsed)
(%i13) mat_trace(A . A)/2
Evaluation took 8.2000 seconds (8.2000 elapsed)
(%o13) 148
(%i14) mat_trace(dgemm(A,A))/2
Evaluation took 7.2100 seconds (7.2100 elapsed)
(%o14) 148.0
(%i15) A:adjacency_matrix(random_graph(400,0.003))
Evaluation took 0.0400 seconds (0.0400 elapsed)
(%i16) mat_trace(A . A)/2
Evaluation took 8.1600 seconds (8.1600 elapsed)
(%o16) 231
(%i17) mat_trace(dgemm(A,A))/2
Evaluation took 9.0900 seconds (9.0900 elapsed)
(%o17) 231.0
(%i18) A:adjacency_matrix(random_graph(900,0.0))
Evaluation took 0.3300 seconds (0.3300 elapsed)
(%i19) mat_trace(A . A)/2
Evaluation took 93.8300 seconds (93.8300 elapsed)
(%o19) 0
(%i20) mat_trace(dgemm(A,A))/2
Evaluation took 21.3900 seconds (21.3900 elapsed)
(%o20) 0.0
(%i21) A:adjacency_matrix(random_graph(900,0.001))
Evaluation took 0.3900 seconds (0.3900 elapsed)
(%i22) mat_trace(A . A)/2
Evaluation took 94.3000 seconds (94.3000 elapsed)
(%o22) 412
(%i23) mat_trace(dgemm(A,A))/2
Evaluation took 41.8600 seconds (41.8600 elapsed)
(%o23) 412.0
(%i24) A:adjacency_matrix(random_graph(900,0.002))
Evaluation took 0.4200 seconds (0.4200 elapsed)
(%i25) mat_trace(A . A)/2
Evaluation took 94.3300 seconds (94.3300 elapsed)
(%o25) 784
(%i26) mat_trace(dgemm(A,A))/2
Evaluation took 62.2200 seconds (62.2200 elapsed)
(%o26) 784.0
(%i27) A:adjacency_matrix(random_graph(900,0.003))
Evaluation took 0.4300 seconds (0.4300 elapsed)
(%i28) mat_trace(A . A)/2
Evaluation took 94.3000 seconds (94.3000 elapsed)
(%o28) 1197
(%i29) mat_trace(dgemm(A,A))/2
Evaluation took 85.6300 seconds (85.6300 elapsed)
(%o29) 1197.0