From fac85ca279fc124ae865f62245e2c7d608d1afc5 Mon Sep 17 00:00:00 2001 From: Kees van Kempen Date: Tue, 4 Oct 2022 10:24:21 +0200 Subject: [PATCH] 04: Change back to copying --- Exercise sheet 4/exercise_sheet_04.ipynb | 36 ++++++------------------ 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/Exercise sheet 4/exercise_sheet_04.ipynb b/Exercise sheet 4/exercise_sheet_04.ipynb index d427e97..b7d966c 100644 --- a/Exercise sheet 4/exercise_sheet_04.ipynb +++ b/Exercise sheet 4/exercise_sheet_04.ipynb @@ -916,35 +916,15 @@ "def remains_valid_after_move(x,i,next_position,L):\n", " '''Returns True if replacing x[i] by next_position would yield a valid configuration,\n", " otherwise False.'''\n", - " # We need to ensure that x is not changed after this function,\n", - " # as alterations done on x will affect x outside the function.\n", - " \n", - " #current_position = np.copy(x[i])\n", - " current_position = x[i].copy()\n", - " #print(type(current_position))\n", - " x[i] = next_position\n", - " ret = disk_config_valid(x, L)\n", - " x[i] = current_position\n", - " return ret\n", - " \n", - " # An alternative approach was to fully copy x, but that turned\n", - " # out to be a lot slower:\n", - " #copy_x = np.copy(x)\n", - " #copy_x[i] = next_position\n", - " #return disk_config_valid(copy_x, L)\n", - " \n", - " # This was tested using the following code.\n", + " # We need to create a copy as not to alter the original x\n", + " # outside this function.\n", " #\n", - " # L = 11.3\n", - " # N = 20\n", - " # delta = .3\n", - " # x = generate_initial_positions(N, L)\n", - " # %timeit [MH_disk_move(x, L, delta) for _ in range(1000)]\n", - " #\n", - " # For the full copy method:\n", - " # 2.05 s ± 46.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n", - " # For the current method, changing x[i] back:\n", - " # 138 ms ± 12.3 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)" + " # NOTE: The copying in this function is the heaviest operation.\n", + " # Reducing this overhead would be great, but I am not familiar\n", + " # enough with references/pointers in Python.\n", + " copy_x = np.copy(x)\n", + " copy_x[i] = next_position\n", + " return disk_config_valid(copy_x, L)" ] }, {