• API
• FAQ
• Tools
• Archive
SHARE
TWEET

Untitled

a guest Jun 20th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. data = Table[RandomReal[], {x, 1, 1000000}];
2. (* Method 1 *)
3. Timing[Total[Table[Total[ Table[data[[i]], {i, j, 10 + j}]], {j, 1, Length[data] - 5*10}]]]
4.
5. (* Method 2, with constant index *)
6. m = 10;
7. Timing[Total[Table[Total[ Table[data[[i]], {i, j, m + j}]], {j, 1, Length[data] - 5*m}]]]
8.
9. Timing[Total[Table[  (Total[ Table[data[[i]], {i, j, m + j}]])^2   , {j, 1, Length[data] - 5*m}]]]
10.
11. Timing[Total[Table[Total[Table[data[[i]], {i, j, 10 + j}]], {j, 1,Length[data] - 5*10}]]]
12.
13. m = 10;
14. Timing[Total[Table[Total[Table[data[[i]], {i, j, m + j}]], {j, 1, Length[data] - 5*10}]]]
15.
16. With[{m = 10},
17.  Timing[Total[Table[Total[Table[data[[i]], {i, j, m + j}]], {j, 1,Length[data] - 5*m}]]]
18.  ]
19.
20. a = With[{m = 10},
21.      Total[
22.       Table[Total[Table[data[[i]], {i, j, m + j}]], {j, 1,
23.         Length[data] - 5*m}]]
24.      ]; // RepeatedTiming // First
25. b = Total@ListCorrelate[ConstantArray[1., m + 1],
26.       data[[;; -50 + m - 1]]]; // RepeatedTiming // First
27. c = Plus[
28.      Range[1., m].data[[1 ;; m]],
29.      (m + 1) Total[data[[m + 1 ;; -5*m - 1]]],
30.      Range[N@m, 1., -1].data[[-5 m ;; -4 m - 1]]
31.      ]; // RepeatedTiming // First
32.
33. a == b == c
34.
35. m=10;
36. Total @ ListCorrelate[ConstantArray[1,m+1], data[[;;-50+m-1]]] //AbsoluteTiming
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?