{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Timing Julia\n", "`@time` is the simplest way to measure execution times" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@time sqrt.(rand(1000));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`@time` can also be used for any expression" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s=0\n", "@time for i=1:1000\n", " global s=s+sqrt(i)\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`@timev` provides additoinal information" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@timev sqrt.(rand(1000));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`@time` returns the value of the expression" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum(@time sqrt.(rand(1000)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`@elapsed` returns the elapsed execution time as a value ..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@elapsed sqrt.(rand(1000))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... and hence can be used in automated tests" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using Test\n", "@test @elapsed(sqrt.(rand(1000))) <= 10e-4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Profiling" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load the `Profile` stdlib module" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using Profile" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We profile a simple function that creates a random matrix, and then\n", "computes the mean of the square of each row." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using Statistics\n", "function randmsq()\n", " x = rand(10000, 1000)\n", " y = mean(x.^2, dims = 1)\n", " return y\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the function once before profiling, to compile it" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "randmsq()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Execute the function while profiling it" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@profile randmsq()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`print` displays a tree view of the execution traces" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Profile.print()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Clear saved profiles" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Profile.clear()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the `init` function to configure the profiler" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Profile.init(delay=.01)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the profile using new setting" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@profile randmsq()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Profileview can be used to visualise the\n", "`] add ProfileView` to add the package" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using Pkg\n", "Pkg.add(\"ProfileView\")\n", "\n", "using ProfileView\n", "ProfileView.view()\n", "ProfileView.svgwrite(\"profile_results.svg\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Statistically significant benchmarking\n", "`]add BenchmarkTools` to add the package" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Pkg.add(\"BenchmarkTools\")\n", "using BenchmarkTools\n", "@benchmark sqrt.(rand(1000))\n", "\n", "@btime mean(rand(1000));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*This notebook was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.6.0-rc3", "language": "julia", "name": "julia-1.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.6.5" } }, "nbformat": 4, "nbformat_minor": 3 }