{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Comparison speed of matrix oprations for Julia and numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Julia\n", "### Measurement how long Julia needs for dotproducts, summation and matrix multiplications" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note:** uncomment the commented lines in the following cell if you run this notebook the first time:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "#using Pkg # activate Package manager\n", "#Pkg.add(\"LinearAlgebra\") # install Package LinearAlgebra, if not yet installed\n", "using LinearAlgebra # use Package LinearAlgebra" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Int64,2}:\n", " 56 84 112\n", " 84 126 168\n", " 112 168 224" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "AA = [2 4 6; 3 6 9; 4 8 12] # 3x3 test matrix\n", "AA * copy(transpose(AA)) # multiply matrix with its transposed" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "LoopJulia (generic function with 1 method)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function LoopJulia(n, x)\n", " b = fill(1.0, (n, n)) # n x n matrix with 1.0\n", " for i in 1:x\n", " b * transpose(b) # matrix multiplication\n", " sum(b) # sum of matrix\n", " end\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run 4 times for minimal statistics" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n" ] }, { "data": { "text/plain": [ "4×2 Array{Float64,2}:\n", " 10.0 2.79997e-5\n", " 100.0 0.00068605\n", " 1000.0 0.128796\n", " 10000.0 72.5904" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "JuliaTimes = fill(0.0, (4, 2)) # 4 x 2 matrix with zeros\n", "for j in 1:4\n", " println(j)\n", " for i in 1:4\n", " n = 10^i # 10, 100, 1000, 10000\n", " elapsed = @elapsed LoopJulia(n, 10) # elapsed time for calculation\n", " JuliaTimes[i, 1] = n\n", " JuliaTimes[i, 2] = JuliaTimes[i, 2] + elapsed\n", " end\n", "end\n", "for i in 1:size(JuliaTimes, 1)\n", " JuliaTimes[i, 2] = JuliaTimes[i, 2] / 4 # divide times by 4\n", "end\n", "JuliaTimes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### _Test dot product_:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n=100 : 1.49975e-6\n", "n=1000 : 1.2475e-7\n", "n=10000 : 1.4749999999999999e-6\n", "n=100000 : 8.245e-5\n", "n=1000000 : 0.000898125\n", "n=10000000 : 0.01285762475\n", "n=100000000 : 0.10946427525\n" ] } ], "source": [ "JuliaTimes2 = fill(0.0, (8, 1))\n", "for j in 1:4\n", " for i in 2:8\n", " n = 10^i\n", " x = rand(n)\n", " y = rand(n)\n", " elapsed = @elapsed sum(dot(x, y));\n", " JuliaTimes2[i] = JuliaTimes2[i] + elapsed;\n", " end\n", "end\n", "for i in 2:8\n", " n = 10^i\n", " println(\"n=$n : \", JuliaTimes2[i] / 4)\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## numpy\n", "### Measurement how long numpy needs for dotproducts, summation and matrix multiplications, when numpy is called via PyCall" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note:** uncomment the commented lines in the following cell if you run this notebook the first time:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "#Pkg.add(\"PyCall\") # install PyCall\n", "using PyCall\n", "#Pkg.add(\"Conda\") # install Conda\n", "#using Conda # use Conda\n", "#Conda.add(\"numpy\") # install numpy via Conda" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numpy = pyimport(\"numpy\") # import numpy" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "numpy.sum(AA) = 54\n" ] }, { "data": { "text/plain": [ "3×3 Array{Int64,2}:\n", " 56 84 112\n", " 84 126 168\n", " 112 168 224" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Test, that numpy commands work\n", "AA = [2 4 6; 3 6 9; 4 8 12] # 3x3 test matrix\n", "@show numpy.sum(AA) # @show outputs result of the line explicitly\n", "numpy.matmul(AA, numpy.transpose(AA))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "LoopNumpy (generic function with 1 method)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function LoopNumpy(n, x)\n", " b = numpy.ones((n, n))\n", " for i in 1:x\n", " numpy.matmul(b, numpy.transpose(b)) # matrix multiplication\n", " numpy.sum(b) # sum of matrix\n", " end\n", "end" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n" ] }, { "data": { "text/plain": [ "4×2 Array{Float64,2}:\n", " 10.0 0.00293235\n", " 100.0 0.00340325\n", " 1000.0 0.372103\n", " 10000.0 115.155" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "NumpyTimes = numpy.zeros((4, 2)) # 4 x 2 matrix with zeros\n", "for j in 1:4\n", " println(j)\n", " for i in 1:4\n", " n = 10^i # 10, 100, 1000, 10000\n", " elapsed = @elapsed LoopNumpy(n, 10) # elapsed time for calculation\n", " NumpyTimes[i, 1] = n\n", " NumpyTimes[i, 2] = NumpyTimes[i, 2] + elapsed\n", " end\n", "end\n", "for i in 1:size(NumpyTimes, 1)\n", " NumpyTimes[i, 2] = NumpyTimes[i, 2] / 4 # divide times by 4\n", "end\n", "NumpyTimes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### _Test dot product_:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n=100 : 5.997525e-5\n", "n=1000 : 6.1675e-5\n", "n=10000 : 5.1525e-5\n", "n=100000 : 0.00016962575\n", "n=1000000 : 0.0012747247500000001\n", "n=10000000 : 0.012431400499999998\n", "n=100000000 : 0.1128788245\n" ] } ], "source": [ "NumpyTimes2 = numpy.zeros((8, 1))\n", "for j in 1:4\n", " for i in 2:8\n", " n = numpy.power(10, i)\n", " x = numpy.random.rand(n)\n", " y = numpy.random.rand(n)\n", " elapsed = @elapsed numpy.sum(numpy.dot(x, y))\n", " NumpyTimes2[i] = NumpyTimes2[i] + elapsed;\n", " end\n", "end\n", "for i in 2:8\n", " n = numpy.power(10, i)\n", " println(\"n=$n : \", NumpyTimes2[i] / 4)\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Result" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Julia, matrix multiplication: 72.59044335\n", "numpy, matrix multiplication: 115.15525460024999\n", "Julia, dot product: 0.10946427525\n", "numpy, dot product: 0.1128788245\n" ] } ], "source": [ "println(\"Julia, matrix multiplication: \", JuliaTimes[4, 2])\n", "println(\"numpy, matrix multiplication: \", NumpyTimes[4, 2])\n", "println(\"Julia, dot product: \", JuliaTimes2[8] / 4)\n", "println(\"numpy, dot product: \", NumpyTimes2[8] / 4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.4", "language": "julia", "name": "julia-1.3" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.4.0" } }, "nbformat": 4, "nbformat_minor": 4 }