{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Time Magics\n", "\n", "There are magics for timing execution of code with a similar syntax to the standard ones in Python." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%time 2**128" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You are able to execute few statements in single line." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%time a = [10,20,30,40,50]; a= a.sum(); a*10;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Time measurement in cell mode" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time \n", "a = [10,20,30,40,50]; \n", "a= a.sum(); \n", "a*10;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Function `%time` provides very basic timing functionality. \n", "Use the timeit magic for more control over the measurement. \n", "Time execution of a Python statement or expression \n", "\n", "Usage, in line mode:\n", "\n", "```%timeit [-n -r -q] statement```\n", "\n", "or in cell mode:\n", "\n", "```%%timeit [-n -r -q] \n", "code \n", "code...```\n", "\n", "Options: \n", "-n: execute the given statement times in a loop. If this value is not given, a fitting value is chosen. \n", "-r: repeat the loop iteration times and take the best result. Default: 3 \n", "-q: Quiet, do not print result.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%timeit -n10 -r3 sin(100)*cos(123)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%timeit -r3 sin(100)*cos(123)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%timeit -r3 -n1 sleep(1000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit -r3 -n10\n", "a = 15;\n", "b = 111;\n", "a * b;" ] } ], "metadata": { "kernelspec": { "display_name": "Groovy", "language": "groovy", "name": "groovy" }, "language_info": { "codemirror_mode": "groovy", "file_extension": ".groovy", "mimetype": "", "name": "Groovy", "nbconverter_exporter": "", "version": "2.4.3" } }, "nbformat": 4, "nbformat_minor": 2 }