speedup using matrices