{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Scaling behaviour\n\nCompute the autocorrelation for varying time series lengths.\n\nThe figure displays the timing and a $N log N$ scaling law, demonstrating\nthe claimed complexity.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport tidynamics\nimport matplotlib.pyplot as plt\nimport time\n\nplt.rcParams['figure.figsize'] = 5.12, 3.84\nplt.rcParams['figure.subplot.bottom'] = 0.18\nplt.rcParams['figure.subplot.left'] = 0.16\n\n\nall_N = []\nN = 64\nfor i in range(14):\n    all_N.append(N + int(N/3))\n    all_N.append(N + int(N/2))\n    all_N.append(N + int(2*N/3))\n    N = 2*N\nall_N = np.array(all_N)\n\nmax_direct_N = 32768\nall_time = []\ndirect_time = []\nn_runs = 5\n\nfor N in all_N:\n    t = 0\n    direct_t = 0\n    for i in range(n_runs):\n        data = np.random.random(size=N)\n        t0 = time.time()\n        acf = tidynamics.acf(data)\n        t += time.time() - t0\n        if N <= max_direct_N:\n            t0 = time.time()\n            acf = np.correlate(data, data, mode='full')\n            direct_t += time.time() - t0\n\n    all_time.append(t/n_runs)\n    if N <= max_direct_N:\n        direct_time.append(direct_t/n_runs)\n\nplt.plot(all_N, all_time, label='actual compute time')\nplt.plot(all_N,\n         all_time[-1] * all_N*np.log(all_N) / (all_N[-1]*np.log(all_N[-1])),\n         label=r'$N\\log N$ scaling')\nplt.plot(all_N[:len(direct_time)], direct_time,\n         label='np.correlate compute time', ls='--')\n\ndata_len = len(direct_time)\nplt.plot(all_N[:data_len],\n         all_N[:data_len]**2 * direct_time[data_len-1] / all_N[data_len-1]**2,\n         label=r'$N^2$ scaling', ls=':')\n\nplt.xlabel(r'$N$')\nplt.ylabel('time')\n\nplt.loglog()\nplt.legend()\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}