{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Installing packages:\n", "\t.package(url: \"https://github.com/bhansconnect/SwiftTimeit\", from: \"1.0.0\")\n", "\t\tSwiftTimeit\n", "With SwiftPM flags: []\n", "Working in: /tmp/tmp_ymcasf6/swift-install\n", "Fetching https://github.com/bhansconnect/SwiftTimeit\n", "Completed resolution in 0.70s\n", "Cloning https://github.com/bhansconnect/SwiftTimeit\n", "Resolving https://github.com/bhansconnect/SwiftTimeit at 1.0.0\n", "[1/2] Compiling SwiftTimeit SwiftTimeit.swift\n", "[2/3] Merging module SwiftTimeit\n", "[3/4] Compiling jupyterInstalledPackages jupyterInstalledPackages.swift\n", "[4/5] Merging module jupyterInstalledPackages\n", "[5/5] Linking libjupyterInstalledPackages.so\n", "Initializing Swift...\n", "Installation complete!\n" ] } ], "source": [ "%install '.package(url: \"https://github.com/bhansconnect/SwiftTimeit\", from: \"1.0.0\")' SwiftTimeit\n", "import SwiftTimeit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Time\n", "Measures the execution of a function once.\n", "\n", "It is not very accurate for short functions. \n", "\n", "You will want to use timeit most of the time." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Execution Time: 115 µs\r\n" ] } ], "source": [ "time{\n", " var x = 0\n", " for _ in 1..<1000000{\n", " x += 1\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Execution Time: 124.72 µs\r\n" ] } ], "source": [ "time(precision: 5){\n", " var x = 0\n", " for _ in 1..<1000000{\n", " x += 1\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Timeit\n", "Accurately measures the execution of a function." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "105 µs ± 13.5 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\r\n" ] } ], "source": [ "timeit{\n", " var x = 0\n", " for _ in 1..<1000000{\n", " x += 1\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "94.5 µs ± 1.96 µs per loop (mean ± std. dev. of 10 runs, 100 loops each)\r\n" ] } ], "source": [ "timeit(loops: 100, repititions: 10){\n", " var x = 0\n", " for _ in 1..<1000000{\n", " x += 1\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The slowest run took 6.63 times longer than the fastest. This could mean that an intermediate result is being cached.\r\n", "617 µs ± 294 µs per loop (mean ± std. dev. of 7 runs, 1 loops each)\r\n" ] } ], "source": [ "var x: UInt32 = 1\n", "timeit(loops: 1){\n", " x = (x + 1) % 10\n", " usleep(x*100)\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": "Swift", "language": "swift", "name": "swift" }, "language_info": { "file_extension": ".swift", "mimetype": "text/x-swift", "name": "swift", "version": "" } }, "nbformat": 4, "nbformat_minor": 2 }