Mathematica code execution speed testing












3














I am testing the speed of Mathematica's latest version against a VB macro for simple loops to evaluate Pi.



So the VB macro is:



c = 0: n = 1000000: m = n * n

For i = 0 To n

For j = 0 To n

If (i * i + j * j) / m < 1 Then

c = c + 1

End If

Next

Next

Selection.Text = 4 * c / m


Now I know how to write the corresponding code in Mathematica, but I do not know the intricacies of Mathematica regarding faster program execution. Can you help please by giving me an example of best optimized code for this example?



There are approximately 4 trillion operations in this example and the VB macro takes less than a day to execute...



This program partitions a quarter of a circular area of radius = 1 to smaller squares of area $1/n^2$ then counts them and adds them together. Essentially, a Monte Carlo Method but with randomness removed.










share|improve this question




















  • 1




    There is a buildt in benchmark function in Wolfram, see reference.wolfram.com/language/Benchmarking/ref/Benchmark.html. An alternative comparison would be to do the same calculations in VB.
    – FredrikD
    8 hours ago










  • "Now I know how to write the corresponding code in Mathematica" ... so include your Mathematica code in the question. Copy and paste in Raw InputForm and format as a code block.
    – Bob Hanlon
    7 hours ago










  • Ok Bob, sorry my mistake, I've been writing code for Mathematica since 2002 and Mathematica 4, what do you think? Mathematica 4 was waayyyyy slower then....
    – dimachaerus
    7 hours ago












  • Erm. The result of the code seems to be c=1 and that takes an unmeasurable small amount of time to execute. That is eactly one integer operation. But I am not fluent in Visual Basic, so I could be wrong. In total, I do not understand your request.
    – Henrik Schumacher
    6 hours ago












  • @Henrik Schumacher Sorry, just edited the code for better speed, now looks ok.
    – dimachaerus
    6 hours ago


















3














I am testing the speed of Mathematica's latest version against a VB macro for simple loops to evaluate Pi.



So the VB macro is:



c = 0: n = 1000000: m = n * n

For i = 0 To n

For j = 0 To n

If (i * i + j * j) / m < 1 Then

c = c + 1

End If

Next

Next

Selection.Text = 4 * c / m


Now I know how to write the corresponding code in Mathematica, but I do not know the intricacies of Mathematica regarding faster program execution. Can you help please by giving me an example of best optimized code for this example?



There are approximately 4 trillion operations in this example and the VB macro takes less than a day to execute...



This program partitions a quarter of a circular area of radius = 1 to smaller squares of area $1/n^2$ then counts them and adds them together. Essentially, a Monte Carlo Method but with randomness removed.










share|improve this question




















  • 1




    There is a buildt in benchmark function in Wolfram, see reference.wolfram.com/language/Benchmarking/ref/Benchmark.html. An alternative comparison would be to do the same calculations in VB.
    – FredrikD
    8 hours ago










  • "Now I know how to write the corresponding code in Mathematica" ... so include your Mathematica code in the question. Copy and paste in Raw InputForm and format as a code block.
    – Bob Hanlon
    7 hours ago










  • Ok Bob, sorry my mistake, I've been writing code for Mathematica since 2002 and Mathematica 4, what do you think? Mathematica 4 was waayyyyy slower then....
    – dimachaerus
    7 hours ago












  • Erm. The result of the code seems to be c=1 and that takes an unmeasurable small amount of time to execute. That is eactly one integer operation. But I am not fluent in Visual Basic, so I could be wrong. In total, I do not understand your request.
    – Henrik Schumacher
    6 hours ago












  • @Henrik Schumacher Sorry, just edited the code for better speed, now looks ok.
    – dimachaerus
    6 hours ago
















3












3








3







I am testing the speed of Mathematica's latest version against a VB macro for simple loops to evaluate Pi.



So the VB macro is:



c = 0: n = 1000000: m = n * n

For i = 0 To n

For j = 0 To n

If (i * i + j * j) / m < 1 Then

c = c + 1

End If

Next

Next

Selection.Text = 4 * c / m


Now I know how to write the corresponding code in Mathematica, but I do not know the intricacies of Mathematica regarding faster program execution. Can you help please by giving me an example of best optimized code for this example?



There are approximately 4 trillion operations in this example and the VB macro takes less than a day to execute...



This program partitions a quarter of a circular area of radius = 1 to smaller squares of area $1/n^2$ then counts them and adds them together. Essentially, a Monte Carlo Method but with randomness removed.










share|improve this question















I am testing the speed of Mathematica's latest version against a VB macro for simple loops to evaluate Pi.



So the VB macro is:



c = 0: n = 1000000: m = n * n

For i = 0 To n

For j = 0 To n

If (i * i + j * j) / m < 1 Then

c = c + 1

End If

Next

Next

Selection.Text = 4 * c / m


Now I know how to write the corresponding code in Mathematica, but I do not know the intricacies of Mathematica regarding faster program execution. Can you help please by giving me an example of best optimized code for this example?



There are approximately 4 trillion operations in this example and the VB macro takes less than a day to execute...



This program partitions a quarter of a circular area of radius = 1 to smaller squares of area $1/n^2$ then counts them and adds them together. Essentially, a Monte Carlo Method but with randomness removed.







performance-tuning






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 4 hours ago









Ruslan

3,25911241




3,25911241










asked 9 hours ago









dimachaerus

225




225








  • 1




    There is a buildt in benchmark function in Wolfram, see reference.wolfram.com/language/Benchmarking/ref/Benchmark.html. An alternative comparison would be to do the same calculations in VB.
    – FredrikD
    8 hours ago










  • "Now I know how to write the corresponding code in Mathematica" ... so include your Mathematica code in the question. Copy and paste in Raw InputForm and format as a code block.
    – Bob Hanlon
    7 hours ago










  • Ok Bob, sorry my mistake, I've been writing code for Mathematica since 2002 and Mathematica 4, what do you think? Mathematica 4 was waayyyyy slower then....
    – dimachaerus
    7 hours ago












  • Erm. The result of the code seems to be c=1 and that takes an unmeasurable small amount of time to execute. That is eactly one integer operation. But I am not fluent in Visual Basic, so I could be wrong. In total, I do not understand your request.
    – Henrik Schumacher
    6 hours ago












  • @Henrik Schumacher Sorry, just edited the code for better speed, now looks ok.
    – dimachaerus
    6 hours ago
















  • 1




    There is a buildt in benchmark function in Wolfram, see reference.wolfram.com/language/Benchmarking/ref/Benchmark.html. An alternative comparison would be to do the same calculations in VB.
    – FredrikD
    8 hours ago










  • "Now I know how to write the corresponding code in Mathematica" ... so include your Mathematica code in the question. Copy and paste in Raw InputForm and format as a code block.
    – Bob Hanlon
    7 hours ago










  • Ok Bob, sorry my mistake, I've been writing code for Mathematica since 2002 and Mathematica 4, what do you think? Mathematica 4 was waayyyyy slower then....
    – dimachaerus
    7 hours ago












  • Erm. The result of the code seems to be c=1 and that takes an unmeasurable small amount of time to execute. That is eactly one integer operation. But I am not fluent in Visual Basic, so I could be wrong. In total, I do not understand your request.
    – Henrik Schumacher
    6 hours ago












  • @Henrik Schumacher Sorry, just edited the code for better speed, now looks ok.
    – dimachaerus
    6 hours ago










1




1




There is a buildt in benchmark function in Wolfram, see reference.wolfram.com/language/Benchmarking/ref/Benchmark.html. An alternative comparison would be to do the same calculations in VB.
– FredrikD
8 hours ago




There is a buildt in benchmark function in Wolfram, see reference.wolfram.com/language/Benchmarking/ref/Benchmark.html. An alternative comparison would be to do the same calculations in VB.
– FredrikD
8 hours ago












"Now I know how to write the corresponding code in Mathematica" ... so include your Mathematica code in the question. Copy and paste in Raw InputForm and format as a code block.
– Bob Hanlon
7 hours ago




"Now I know how to write the corresponding code in Mathematica" ... so include your Mathematica code in the question. Copy and paste in Raw InputForm and format as a code block.
– Bob Hanlon
7 hours ago












Ok Bob, sorry my mistake, I've been writing code for Mathematica since 2002 and Mathematica 4, what do you think? Mathematica 4 was waayyyyy slower then....
– dimachaerus
7 hours ago






Ok Bob, sorry my mistake, I've been writing code for Mathematica since 2002 and Mathematica 4, what do you think? Mathematica 4 was waayyyyy slower then....
– dimachaerus
7 hours ago














Erm. The result of the code seems to be c=1 and that takes an unmeasurable small amount of time to execute. That is eactly one integer operation. But I am not fluent in Visual Basic, so I could be wrong. In total, I do not understand your request.
– Henrik Schumacher
6 hours ago






Erm. The result of the code seems to be c=1 and that takes an unmeasurable small amount of time to execute. That is eactly one integer operation. But I am not fluent in Visual Basic, so I could be wrong. In total, I do not understand your request.
– Henrik Schumacher
6 hours ago














@Henrik Schumacher Sorry, just edited the code for better speed, now looks ok.
– dimachaerus
6 hours ago






@Henrik Schumacher Sorry, just edited the code for better speed, now looks ok.
– dimachaerus
6 hours ago












4 Answers
4






active

oldest

votes


















4














You're comparing a compiled language with an interpreted one. Or, at least, you are using the Wolfram language in interpreted mode. You'll have better results if you use a Compiled function.



Example without Compile — directly translated code:



calcNaive = Function[{n},
Module[{c = 0., m = N[n^2]},
Do[
Do[
If[(i*i + j*j)/m < 1.,
++c
]
, {i, 0., n}]
, {j, 0., n}];
4. c/m]];
AbsoluteTiming@calcNaive[10000]



{264.352184, 3.14199016}




First value in the output is timing in seconds, seconds is the function return value.



Note here the N[n^2] instead of simple n^2. This way we avoid using arbitrary-length integers in further code, which would result in a 17% longer computation for no real benefit. In general, if you don't need advanced precision, you'd better enter all your numbers as machine-precision numbers, e.g. 1000.0 instead of 1000.



Now the code using Compile (which always uses machine-precision numbers, in addition to C-language-compiler optimizations):



calcCompiled = Compile[{n},
Module[{c = 0, m = n^2},
Do[
Do[
If[(i*i + j*j)/m < 1,
++c
]
, {i, 0, n}]
, {j, 0, n}];
4 c/m]
, CompilationTarget -> "C"
, RuntimeOptions -> "Speed"];
AbsoluteTiming@calcCompiled[10000]



{0.918117, 3.14199016}




Notice more than two orders of magnitude faster calculation.






share|improve this answer























  • Thanks. The right answer I was looking for.
    – dimachaerus
    3 hours ago



















5














This is an algorithm of complexity $O(n)$ instead of $O(n^2)$. It runs through in about 8 milliseconds.



n = 1000000;
piapprox =
Total[Floor[
Sqrt[Ramp[Subtract[N[n^2] - 1., Range[1., n]^2]]]]] (4./n^2); //
RepeatedTiming // First

piapprox - Pi



0.00812



-4.00401*10^-6




Moreover, you get more bang for your bucks with the trapezoidal rule as follows:



n = 1000000;
weights = ConstantArray[1./n, n + 1];
weights[[{1, -1}]] = 0.5/n;
piapprox2 = 4. (weights.Sqrt[1. - Subdivide[0., 1., n]^2]);
piapprox2 - Pi



-1.17598*10^-9




And this computes Pi with 16-digit precision by approximating the intersection of the unit disk with the first quadrant by $2^k$ congruent triangles; the area of a single triangle is computed (one half of the result of Det) and multiplied by $4, 2^k$.



RepeatedTiming[
iters = 25;
{p, q} = N[IdentityMatrix[2]];
piapprox = 2^(k + 1) Det[{p, Nest[x [Function] Normalize[p + x], q, iters]}];
][[1]]
piapprox - Pi



0.000048



4.44089*10^-16




This uncompiled versions needs about 0.04 milliseconds.






share|improve this answer























  • Sorry, I am not looking for fast methods to evaluate Pi. I am testing the speed of Mathematica for the particular loop example against Visual Basic and I am finding Mathematica to be very slow compared.
    – dimachaerus
    4 hours ago






  • 1




    It is slower in Mathematica because you did it the wrong way. For is well-known to be very slow when not compiled. Try Do instead.
    – Henrik Schumacher
    4 hours ago










  • No, even simple: Do[ i * i + j * j, {i, 0, 10000}, {j, 0, 10000}] takes more time than VB.
    – dimachaerus
    4 hours ago





















3














Here's another comparison. First let's look at the raw, inefficient version that someone who comes to Mathematica from another language might try:



badForMathematica[n_] :=
Module[{i, j, c = 0., m = n^2},
Do[
If[(i^2 + j^2)/m < 1,
c = c + 1
],
{i, 0, n - 1},
{j, 0, n - 1}
];
(4*c)/m
]

badForMathematica[1000] // AbsoluteTiming

{3.07878, 3.14552}


Eeesh. But this is the dumb way. Here's a new approach. First we take the your algorithm and realize you're just subdividing and counting hits inside the circle. We can do this much faster using vectorized operations. Here's a way to get all the hits:



countHits[{i1_, i2_}, {j1_, j2_}, m_] :=

With[{r1 = Range[i1, i2]^2, r2 = Range[j1, j2]^2},
Total@UnitStep[m - Join @@ Outer[Plus, r1, r2]]
];


We generate the Outer product you're really generating, then totaling the counts. Simple as that. Now we cook this into a larger algorithm where we work in chunks because I don't have enough memory to work all at once:



goodForMathematica[n_, chunkSize_: 1000] :=
Module[{m, c = 0, chunks},
m = n^2;
chunks =
If[chunkSize >= n,
{{0, n}},
If[#[[-1]] =!= n,
Append[#, n],
#
] &@Range[0, n, chunkSize]
];
Do[c += countHits[c1, c2, m], {c1, chunks}, {c2, chunks}];
(4.*c)/m
]


If you have lots of memory just do it straight out (but you probably don't have enough). Here's that test:



goodForMathematica[10000] // AbsoluteTiming

{3.37823, 3.14199}


We increased the count by an order of magnitude but had similar performance. Note that this algorithm has trash scaling, so we can expect that n = 20000 will take ~12s because we've got four blocks to work with:



1000000 will therefore take about ~35000s or ~10 hours. Not gonna actually do it.



Finally, here's probably the first thing to think of, which is to just use Compile:



compiledVersion =
Compile[{{n, _Integer}},
Module[{i, j, c = 0., m = n^2},
Do[
If[(i^2 + j^2)/m < 1,
c = c + 1
],
{i, 0, n - 1},
{j, 0, n - 1}
];
(4*c)/m
],
RuntimeOptions -> "Speed",
CompilationTarget -> "C"
];

compiledVersion[100000] // AbsoluteTiming

{14.8691, 3.14163}


And this blows everything else out of the water (notice I use 100000 per grid subdivision), but that makes sense, because we're really using C, not Mathematica.






share|improve this answer





















  • There's no need to put i and j into Module: Do already localizes their names.
    – Ruslan
    3 hours ago










  • @Ruslan ah yeah at first I was just directly copying the OPs For structure but then got annoyed with it. I'll prune it sometime later.
    – b3m2a1
    3 hours ago










  • Excellent answer! I noticed that the "i" outer loop can be further subdivided into n separate smaller loops to utilize the n different cores of my processor and kernels of Mathematica 11.3 for parallel processing. Any idea how to optimize code for this task?
    – dimachaerus
    1 hour ago





















1














Initialize the variables



c = 0;
n = 1000000;


Below is a more or less literal translation of the Visual Basic code.



check[i_, j_] := If[(i*i + j*j)/(n*n) < 1, c++]

Timing[
Scan[
Function[i,
Scan[
Function[j,
check[i, j]
],
Range[n]
]
],
Range[n]
]
]


A slightly faster verion gets rid of the overhead caused by the check function and the computation of Range[n] as well as n*n (suggested by dimachaerus comment).



nList = Range[n];
n2 = n*n;

Timing[
Scan[
Function[i,
Scan[
Function[j,
If[(i*i + j*j)/n2 < 1, c++]
],
nList
]
],
nList
]
]


The answers from the two experiments are: (to be inserted when complete)






share|improve this answer























  • Jack, get rid of n * n evaluation each time and instead replace with constant m = n * n
    – dimachaerus
    6 hours ago












  • Ok, but still very very slow compared to VB.
    – dimachaerus
    4 hours ago











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188423%2fmathematica-code-execution-speed-testing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














You're comparing a compiled language with an interpreted one. Or, at least, you are using the Wolfram language in interpreted mode. You'll have better results if you use a Compiled function.



Example without Compile — directly translated code:



calcNaive = Function[{n},
Module[{c = 0., m = N[n^2]},
Do[
Do[
If[(i*i + j*j)/m < 1.,
++c
]
, {i, 0., n}]
, {j, 0., n}];
4. c/m]];
AbsoluteTiming@calcNaive[10000]



{264.352184, 3.14199016}




First value in the output is timing in seconds, seconds is the function return value.



Note here the N[n^2] instead of simple n^2. This way we avoid using arbitrary-length integers in further code, which would result in a 17% longer computation for no real benefit. In general, if you don't need advanced precision, you'd better enter all your numbers as machine-precision numbers, e.g. 1000.0 instead of 1000.



Now the code using Compile (which always uses machine-precision numbers, in addition to C-language-compiler optimizations):



calcCompiled = Compile[{n},
Module[{c = 0, m = n^2},
Do[
Do[
If[(i*i + j*j)/m < 1,
++c
]
, {i, 0, n}]
, {j, 0, n}];
4 c/m]
, CompilationTarget -> "C"
, RuntimeOptions -> "Speed"];
AbsoluteTiming@calcCompiled[10000]



{0.918117, 3.14199016}




Notice more than two orders of magnitude faster calculation.






share|improve this answer























  • Thanks. The right answer I was looking for.
    – dimachaerus
    3 hours ago
















4














You're comparing a compiled language with an interpreted one. Or, at least, you are using the Wolfram language in interpreted mode. You'll have better results if you use a Compiled function.



Example without Compile — directly translated code:



calcNaive = Function[{n},
Module[{c = 0., m = N[n^2]},
Do[
Do[
If[(i*i + j*j)/m < 1.,
++c
]
, {i, 0., n}]
, {j, 0., n}];
4. c/m]];
AbsoluteTiming@calcNaive[10000]



{264.352184, 3.14199016}




First value in the output is timing in seconds, seconds is the function return value.



Note here the N[n^2] instead of simple n^2. This way we avoid using arbitrary-length integers in further code, which would result in a 17% longer computation for no real benefit. In general, if you don't need advanced precision, you'd better enter all your numbers as machine-precision numbers, e.g. 1000.0 instead of 1000.



Now the code using Compile (which always uses machine-precision numbers, in addition to C-language-compiler optimizations):



calcCompiled = Compile[{n},
Module[{c = 0, m = n^2},
Do[
Do[
If[(i*i + j*j)/m < 1,
++c
]
, {i, 0, n}]
, {j, 0, n}];
4 c/m]
, CompilationTarget -> "C"
, RuntimeOptions -> "Speed"];
AbsoluteTiming@calcCompiled[10000]



{0.918117, 3.14199016}




Notice more than two orders of magnitude faster calculation.






share|improve this answer























  • Thanks. The right answer I was looking for.
    – dimachaerus
    3 hours ago














4












4








4






You're comparing a compiled language with an interpreted one. Or, at least, you are using the Wolfram language in interpreted mode. You'll have better results if you use a Compiled function.



Example without Compile — directly translated code:



calcNaive = Function[{n},
Module[{c = 0., m = N[n^2]},
Do[
Do[
If[(i*i + j*j)/m < 1.,
++c
]
, {i, 0., n}]
, {j, 0., n}];
4. c/m]];
AbsoluteTiming@calcNaive[10000]



{264.352184, 3.14199016}




First value in the output is timing in seconds, seconds is the function return value.



Note here the N[n^2] instead of simple n^2. This way we avoid using arbitrary-length integers in further code, which would result in a 17% longer computation for no real benefit. In general, if you don't need advanced precision, you'd better enter all your numbers as machine-precision numbers, e.g. 1000.0 instead of 1000.



Now the code using Compile (which always uses machine-precision numbers, in addition to C-language-compiler optimizations):



calcCompiled = Compile[{n},
Module[{c = 0, m = n^2},
Do[
Do[
If[(i*i + j*j)/m < 1,
++c
]
, {i, 0, n}]
, {j, 0, n}];
4 c/m]
, CompilationTarget -> "C"
, RuntimeOptions -> "Speed"];
AbsoluteTiming@calcCompiled[10000]



{0.918117, 3.14199016}




Notice more than two orders of magnitude faster calculation.






share|improve this answer














You're comparing a compiled language with an interpreted one. Or, at least, you are using the Wolfram language in interpreted mode. You'll have better results if you use a Compiled function.



Example without Compile — directly translated code:



calcNaive = Function[{n},
Module[{c = 0., m = N[n^2]},
Do[
Do[
If[(i*i + j*j)/m < 1.,
++c
]
, {i, 0., n}]
, {j, 0., n}];
4. c/m]];
AbsoluteTiming@calcNaive[10000]



{264.352184, 3.14199016}




First value in the output is timing in seconds, seconds is the function return value.



Note here the N[n^2] instead of simple n^2. This way we avoid using arbitrary-length integers in further code, which would result in a 17% longer computation for no real benefit. In general, if you don't need advanced precision, you'd better enter all your numbers as machine-precision numbers, e.g. 1000.0 instead of 1000.



Now the code using Compile (which always uses machine-precision numbers, in addition to C-language-compiler optimizations):



calcCompiled = Compile[{n},
Module[{c = 0, m = n^2},
Do[
Do[
If[(i*i + j*j)/m < 1,
++c
]
, {i, 0, n}]
, {j, 0, n}];
4 c/m]
, CompilationTarget -> "C"
, RuntimeOptions -> "Speed"];
AbsoluteTiming@calcCompiled[10000]



{0.918117, 3.14199016}




Notice more than two orders of magnitude faster calculation.







share|improve this answer














share|improve this answer



share|improve this answer








edited 3 hours ago

























answered 3 hours ago









Ruslan

3,25911241




3,25911241












  • Thanks. The right answer I was looking for.
    – dimachaerus
    3 hours ago


















  • Thanks. The right answer I was looking for.
    – dimachaerus
    3 hours ago
















Thanks. The right answer I was looking for.
– dimachaerus
3 hours ago




Thanks. The right answer I was looking for.
– dimachaerus
3 hours ago











5














This is an algorithm of complexity $O(n)$ instead of $O(n^2)$. It runs through in about 8 milliseconds.



n = 1000000;
piapprox =
Total[Floor[
Sqrt[Ramp[Subtract[N[n^2] - 1., Range[1., n]^2]]]]] (4./n^2); //
RepeatedTiming // First

piapprox - Pi



0.00812



-4.00401*10^-6




Moreover, you get more bang for your bucks with the trapezoidal rule as follows:



n = 1000000;
weights = ConstantArray[1./n, n + 1];
weights[[{1, -1}]] = 0.5/n;
piapprox2 = 4. (weights.Sqrt[1. - Subdivide[0., 1., n]^2]);
piapprox2 - Pi



-1.17598*10^-9




And this computes Pi with 16-digit precision by approximating the intersection of the unit disk with the first quadrant by $2^k$ congruent triangles; the area of a single triangle is computed (one half of the result of Det) and multiplied by $4, 2^k$.



RepeatedTiming[
iters = 25;
{p, q} = N[IdentityMatrix[2]];
piapprox = 2^(k + 1) Det[{p, Nest[x [Function] Normalize[p + x], q, iters]}];
][[1]]
piapprox - Pi



0.000048



4.44089*10^-16




This uncompiled versions needs about 0.04 milliseconds.






share|improve this answer























  • Sorry, I am not looking for fast methods to evaluate Pi. I am testing the speed of Mathematica for the particular loop example against Visual Basic and I am finding Mathematica to be very slow compared.
    – dimachaerus
    4 hours ago






  • 1




    It is slower in Mathematica because you did it the wrong way. For is well-known to be very slow when not compiled. Try Do instead.
    – Henrik Schumacher
    4 hours ago










  • No, even simple: Do[ i * i + j * j, {i, 0, 10000}, {j, 0, 10000}] takes more time than VB.
    – dimachaerus
    4 hours ago


















5














This is an algorithm of complexity $O(n)$ instead of $O(n^2)$. It runs through in about 8 milliseconds.



n = 1000000;
piapprox =
Total[Floor[
Sqrt[Ramp[Subtract[N[n^2] - 1., Range[1., n]^2]]]]] (4./n^2); //
RepeatedTiming // First

piapprox - Pi



0.00812



-4.00401*10^-6




Moreover, you get more bang for your bucks with the trapezoidal rule as follows:



n = 1000000;
weights = ConstantArray[1./n, n + 1];
weights[[{1, -1}]] = 0.5/n;
piapprox2 = 4. (weights.Sqrt[1. - Subdivide[0., 1., n]^2]);
piapprox2 - Pi



-1.17598*10^-9




And this computes Pi with 16-digit precision by approximating the intersection of the unit disk with the first quadrant by $2^k$ congruent triangles; the area of a single triangle is computed (one half of the result of Det) and multiplied by $4, 2^k$.



RepeatedTiming[
iters = 25;
{p, q} = N[IdentityMatrix[2]];
piapprox = 2^(k + 1) Det[{p, Nest[x [Function] Normalize[p + x], q, iters]}];
][[1]]
piapprox - Pi



0.000048



4.44089*10^-16




This uncompiled versions needs about 0.04 milliseconds.






share|improve this answer























  • Sorry, I am not looking for fast methods to evaluate Pi. I am testing the speed of Mathematica for the particular loop example against Visual Basic and I am finding Mathematica to be very slow compared.
    – dimachaerus
    4 hours ago






  • 1




    It is slower in Mathematica because you did it the wrong way. For is well-known to be very slow when not compiled. Try Do instead.
    – Henrik Schumacher
    4 hours ago










  • No, even simple: Do[ i * i + j * j, {i, 0, 10000}, {j, 0, 10000}] takes more time than VB.
    – dimachaerus
    4 hours ago
















5












5








5






This is an algorithm of complexity $O(n)$ instead of $O(n^2)$. It runs through in about 8 milliseconds.



n = 1000000;
piapprox =
Total[Floor[
Sqrt[Ramp[Subtract[N[n^2] - 1., Range[1., n]^2]]]]] (4./n^2); //
RepeatedTiming // First

piapprox - Pi



0.00812



-4.00401*10^-6




Moreover, you get more bang for your bucks with the trapezoidal rule as follows:



n = 1000000;
weights = ConstantArray[1./n, n + 1];
weights[[{1, -1}]] = 0.5/n;
piapprox2 = 4. (weights.Sqrt[1. - Subdivide[0., 1., n]^2]);
piapprox2 - Pi



-1.17598*10^-9




And this computes Pi with 16-digit precision by approximating the intersection of the unit disk with the first quadrant by $2^k$ congruent triangles; the area of a single triangle is computed (one half of the result of Det) and multiplied by $4, 2^k$.



RepeatedTiming[
iters = 25;
{p, q} = N[IdentityMatrix[2]];
piapprox = 2^(k + 1) Det[{p, Nest[x [Function] Normalize[p + x], q, iters]}];
][[1]]
piapprox - Pi



0.000048



4.44089*10^-16




This uncompiled versions needs about 0.04 milliseconds.






share|improve this answer














This is an algorithm of complexity $O(n)$ instead of $O(n^2)$. It runs through in about 8 milliseconds.



n = 1000000;
piapprox =
Total[Floor[
Sqrt[Ramp[Subtract[N[n^2] - 1., Range[1., n]^2]]]]] (4./n^2); //
RepeatedTiming // First

piapprox - Pi



0.00812



-4.00401*10^-6




Moreover, you get more bang for your bucks with the trapezoidal rule as follows:



n = 1000000;
weights = ConstantArray[1./n, n + 1];
weights[[{1, -1}]] = 0.5/n;
piapprox2 = 4. (weights.Sqrt[1. - Subdivide[0., 1., n]^2]);
piapprox2 - Pi



-1.17598*10^-9




And this computes Pi with 16-digit precision by approximating the intersection of the unit disk with the first quadrant by $2^k$ congruent triangles; the area of a single triangle is computed (one half of the result of Det) and multiplied by $4, 2^k$.



RepeatedTiming[
iters = 25;
{p, q} = N[IdentityMatrix[2]];
piapprox = 2^(k + 1) Det[{p, Nest[x [Function] Normalize[p + x], q, iters]}];
][[1]]
piapprox - Pi



0.000048



4.44089*10^-16




This uncompiled versions needs about 0.04 milliseconds.







share|improve this answer














share|improve this answer



share|improve this answer








edited 5 hours ago

























answered 6 hours ago









Henrik Schumacher

48.3k467137




48.3k467137












  • Sorry, I am not looking for fast methods to evaluate Pi. I am testing the speed of Mathematica for the particular loop example against Visual Basic and I am finding Mathematica to be very slow compared.
    – dimachaerus
    4 hours ago






  • 1




    It is slower in Mathematica because you did it the wrong way. For is well-known to be very slow when not compiled. Try Do instead.
    – Henrik Schumacher
    4 hours ago










  • No, even simple: Do[ i * i + j * j, {i, 0, 10000}, {j, 0, 10000}] takes more time than VB.
    – dimachaerus
    4 hours ago




















  • Sorry, I am not looking for fast methods to evaluate Pi. I am testing the speed of Mathematica for the particular loop example against Visual Basic and I am finding Mathematica to be very slow compared.
    – dimachaerus
    4 hours ago






  • 1




    It is slower in Mathematica because you did it the wrong way. For is well-known to be very slow when not compiled. Try Do instead.
    – Henrik Schumacher
    4 hours ago










  • No, even simple: Do[ i * i + j * j, {i, 0, 10000}, {j, 0, 10000}] takes more time than VB.
    – dimachaerus
    4 hours ago


















Sorry, I am not looking for fast methods to evaluate Pi. I am testing the speed of Mathematica for the particular loop example against Visual Basic and I am finding Mathematica to be very slow compared.
– dimachaerus
4 hours ago




Sorry, I am not looking for fast methods to evaluate Pi. I am testing the speed of Mathematica for the particular loop example against Visual Basic and I am finding Mathematica to be very slow compared.
– dimachaerus
4 hours ago




1




1




It is slower in Mathematica because you did it the wrong way. For is well-known to be very slow when not compiled. Try Do instead.
– Henrik Schumacher
4 hours ago




It is slower in Mathematica because you did it the wrong way. For is well-known to be very slow when not compiled. Try Do instead.
– Henrik Schumacher
4 hours ago












No, even simple: Do[ i * i + j * j, {i, 0, 10000}, {j, 0, 10000}] takes more time than VB.
– dimachaerus
4 hours ago






No, even simple: Do[ i * i + j * j, {i, 0, 10000}, {j, 0, 10000}] takes more time than VB.
– dimachaerus
4 hours ago













3














Here's another comparison. First let's look at the raw, inefficient version that someone who comes to Mathematica from another language might try:



badForMathematica[n_] :=
Module[{i, j, c = 0., m = n^2},
Do[
If[(i^2 + j^2)/m < 1,
c = c + 1
],
{i, 0, n - 1},
{j, 0, n - 1}
];
(4*c)/m
]

badForMathematica[1000] // AbsoluteTiming

{3.07878, 3.14552}


Eeesh. But this is the dumb way. Here's a new approach. First we take the your algorithm and realize you're just subdividing and counting hits inside the circle. We can do this much faster using vectorized operations. Here's a way to get all the hits:



countHits[{i1_, i2_}, {j1_, j2_}, m_] :=

With[{r1 = Range[i1, i2]^2, r2 = Range[j1, j2]^2},
Total@UnitStep[m - Join @@ Outer[Plus, r1, r2]]
];


We generate the Outer product you're really generating, then totaling the counts. Simple as that. Now we cook this into a larger algorithm where we work in chunks because I don't have enough memory to work all at once:



goodForMathematica[n_, chunkSize_: 1000] :=
Module[{m, c = 0, chunks},
m = n^2;
chunks =
If[chunkSize >= n,
{{0, n}},
If[#[[-1]] =!= n,
Append[#, n],
#
] &@Range[0, n, chunkSize]
];
Do[c += countHits[c1, c2, m], {c1, chunks}, {c2, chunks}];
(4.*c)/m
]


If you have lots of memory just do it straight out (but you probably don't have enough). Here's that test:



goodForMathematica[10000] // AbsoluteTiming

{3.37823, 3.14199}


We increased the count by an order of magnitude but had similar performance. Note that this algorithm has trash scaling, so we can expect that n = 20000 will take ~12s because we've got four blocks to work with:



1000000 will therefore take about ~35000s or ~10 hours. Not gonna actually do it.



Finally, here's probably the first thing to think of, which is to just use Compile:



compiledVersion =
Compile[{{n, _Integer}},
Module[{i, j, c = 0., m = n^2},
Do[
If[(i^2 + j^2)/m < 1,
c = c + 1
],
{i, 0, n - 1},
{j, 0, n - 1}
];
(4*c)/m
],
RuntimeOptions -> "Speed",
CompilationTarget -> "C"
];

compiledVersion[100000] // AbsoluteTiming

{14.8691, 3.14163}


And this blows everything else out of the water (notice I use 100000 per grid subdivision), but that makes sense, because we're really using C, not Mathematica.






share|improve this answer





















  • There's no need to put i and j into Module: Do already localizes their names.
    – Ruslan
    3 hours ago










  • @Ruslan ah yeah at first I was just directly copying the OPs For structure but then got annoyed with it. I'll prune it sometime later.
    – b3m2a1
    3 hours ago










  • Excellent answer! I noticed that the "i" outer loop can be further subdivided into n separate smaller loops to utilize the n different cores of my processor and kernels of Mathematica 11.3 for parallel processing. Any idea how to optimize code for this task?
    – dimachaerus
    1 hour ago


















3














Here's another comparison. First let's look at the raw, inefficient version that someone who comes to Mathematica from another language might try:



badForMathematica[n_] :=
Module[{i, j, c = 0., m = n^2},
Do[
If[(i^2 + j^2)/m < 1,
c = c + 1
],
{i, 0, n - 1},
{j, 0, n - 1}
];
(4*c)/m
]

badForMathematica[1000] // AbsoluteTiming

{3.07878, 3.14552}


Eeesh. But this is the dumb way. Here's a new approach. First we take the your algorithm and realize you're just subdividing and counting hits inside the circle. We can do this much faster using vectorized operations. Here's a way to get all the hits:



countHits[{i1_, i2_}, {j1_, j2_}, m_] :=

With[{r1 = Range[i1, i2]^2, r2 = Range[j1, j2]^2},
Total@UnitStep[m - Join @@ Outer[Plus, r1, r2]]
];


We generate the Outer product you're really generating, then totaling the counts. Simple as that. Now we cook this into a larger algorithm where we work in chunks because I don't have enough memory to work all at once:



goodForMathematica[n_, chunkSize_: 1000] :=
Module[{m, c = 0, chunks},
m = n^2;
chunks =
If[chunkSize >= n,
{{0, n}},
If[#[[-1]] =!= n,
Append[#, n],
#
] &@Range[0, n, chunkSize]
];
Do[c += countHits[c1, c2, m], {c1, chunks}, {c2, chunks}];
(4.*c)/m
]


If you have lots of memory just do it straight out (but you probably don't have enough). Here's that test:



goodForMathematica[10000] // AbsoluteTiming

{3.37823, 3.14199}


We increased the count by an order of magnitude but had similar performance. Note that this algorithm has trash scaling, so we can expect that n = 20000 will take ~12s because we've got four blocks to work with:



1000000 will therefore take about ~35000s or ~10 hours. Not gonna actually do it.



Finally, here's probably the first thing to think of, which is to just use Compile:



compiledVersion =
Compile[{{n, _Integer}},
Module[{i, j, c = 0., m = n^2},
Do[
If[(i^2 + j^2)/m < 1,
c = c + 1
],
{i, 0, n - 1},
{j, 0, n - 1}
];
(4*c)/m
],
RuntimeOptions -> "Speed",
CompilationTarget -> "C"
];

compiledVersion[100000] // AbsoluteTiming

{14.8691, 3.14163}


And this blows everything else out of the water (notice I use 100000 per grid subdivision), but that makes sense, because we're really using C, not Mathematica.






share|improve this answer





















  • There's no need to put i and j into Module: Do already localizes their names.
    – Ruslan
    3 hours ago










  • @Ruslan ah yeah at first I was just directly copying the OPs For structure but then got annoyed with it. I'll prune it sometime later.
    – b3m2a1
    3 hours ago










  • Excellent answer! I noticed that the "i" outer loop can be further subdivided into n separate smaller loops to utilize the n different cores of my processor and kernels of Mathematica 11.3 for parallel processing. Any idea how to optimize code for this task?
    – dimachaerus
    1 hour ago
















3












3








3






Here's another comparison. First let's look at the raw, inefficient version that someone who comes to Mathematica from another language might try:



badForMathematica[n_] :=
Module[{i, j, c = 0., m = n^2},
Do[
If[(i^2 + j^2)/m < 1,
c = c + 1
],
{i, 0, n - 1},
{j, 0, n - 1}
];
(4*c)/m
]

badForMathematica[1000] // AbsoluteTiming

{3.07878, 3.14552}


Eeesh. But this is the dumb way. Here's a new approach. First we take the your algorithm and realize you're just subdividing and counting hits inside the circle. We can do this much faster using vectorized operations. Here's a way to get all the hits:



countHits[{i1_, i2_}, {j1_, j2_}, m_] :=

With[{r1 = Range[i1, i2]^2, r2 = Range[j1, j2]^2},
Total@UnitStep[m - Join @@ Outer[Plus, r1, r2]]
];


We generate the Outer product you're really generating, then totaling the counts. Simple as that. Now we cook this into a larger algorithm where we work in chunks because I don't have enough memory to work all at once:



goodForMathematica[n_, chunkSize_: 1000] :=
Module[{m, c = 0, chunks},
m = n^2;
chunks =
If[chunkSize >= n,
{{0, n}},
If[#[[-1]] =!= n,
Append[#, n],
#
] &@Range[0, n, chunkSize]
];
Do[c += countHits[c1, c2, m], {c1, chunks}, {c2, chunks}];
(4.*c)/m
]


If you have lots of memory just do it straight out (but you probably don't have enough). Here's that test:



goodForMathematica[10000] // AbsoluteTiming

{3.37823, 3.14199}


We increased the count by an order of magnitude but had similar performance. Note that this algorithm has trash scaling, so we can expect that n = 20000 will take ~12s because we've got four blocks to work with:



1000000 will therefore take about ~35000s or ~10 hours. Not gonna actually do it.



Finally, here's probably the first thing to think of, which is to just use Compile:



compiledVersion =
Compile[{{n, _Integer}},
Module[{i, j, c = 0., m = n^2},
Do[
If[(i^2 + j^2)/m < 1,
c = c + 1
],
{i, 0, n - 1},
{j, 0, n - 1}
];
(4*c)/m
],
RuntimeOptions -> "Speed",
CompilationTarget -> "C"
];

compiledVersion[100000] // AbsoluteTiming

{14.8691, 3.14163}


And this blows everything else out of the water (notice I use 100000 per grid subdivision), but that makes sense, because we're really using C, not Mathematica.






share|improve this answer












Here's another comparison. First let's look at the raw, inefficient version that someone who comes to Mathematica from another language might try:



badForMathematica[n_] :=
Module[{i, j, c = 0., m = n^2},
Do[
If[(i^2 + j^2)/m < 1,
c = c + 1
],
{i, 0, n - 1},
{j, 0, n - 1}
];
(4*c)/m
]

badForMathematica[1000] // AbsoluteTiming

{3.07878, 3.14552}


Eeesh. But this is the dumb way. Here's a new approach. First we take the your algorithm and realize you're just subdividing and counting hits inside the circle. We can do this much faster using vectorized operations. Here's a way to get all the hits:



countHits[{i1_, i2_}, {j1_, j2_}, m_] :=

With[{r1 = Range[i1, i2]^2, r2 = Range[j1, j2]^2},
Total@UnitStep[m - Join @@ Outer[Plus, r1, r2]]
];


We generate the Outer product you're really generating, then totaling the counts. Simple as that. Now we cook this into a larger algorithm where we work in chunks because I don't have enough memory to work all at once:



goodForMathematica[n_, chunkSize_: 1000] :=
Module[{m, c = 0, chunks},
m = n^2;
chunks =
If[chunkSize >= n,
{{0, n}},
If[#[[-1]] =!= n,
Append[#, n],
#
] &@Range[0, n, chunkSize]
];
Do[c += countHits[c1, c2, m], {c1, chunks}, {c2, chunks}];
(4.*c)/m
]


If you have lots of memory just do it straight out (but you probably don't have enough). Here's that test:



goodForMathematica[10000] // AbsoluteTiming

{3.37823, 3.14199}


We increased the count by an order of magnitude but had similar performance. Note that this algorithm has trash scaling, so we can expect that n = 20000 will take ~12s because we've got four blocks to work with:



1000000 will therefore take about ~35000s or ~10 hours. Not gonna actually do it.



Finally, here's probably the first thing to think of, which is to just use Compile:



compiledVersion =
Compile[{{n, _Integer}},
Module[{i, j, c = 0., m = n^2},
Do[
If[(i^2 + j^2)/m < 1,
c = c + 1
],
{i, 0, n - 1},
{j, 0, n - 1}
];
(4*c)/m
],
RuntimeOptions -> "Speed",
CompilationTarget -> "C"
];

compiledVersion[100000] // AbsoluteTiming

{14.8691, 3.14163}


And this blows everything else out of the water (notice I use 100000 per grid subdivision), but that makes sense, because we're really using C, not Mathematica.







share|improve this answer












share|improve this answer



share|improve this answer










answered 3 hours ago









b3m2a1

26.6k257154




26.6k257154












  • There's no need to put i and j into Module: Do already localizes their names.
    – Ruslan
    3 hours ago










  • @Ruslan ah yeah at first I was just directly copying the OPs For structure but then got annoyed with it. I'll prune it sometime later.
    – b3m2a1
    3 hours ago










  • Excellent answer! I noticed that the "i" outer loop can be further subdivided into n separate smaller loops to utilize the n different cores of my processor and kernels of Mathematica 11.3 for parallel processing. Any idea how to optimize code for this task?
    – dimachaerus
    1 hour ago




















  • There's no need to put i and j into Module: Do already localizes their names.
    – Ruslan
    3 hours ago










  • @Ruslan ah yeah at first I was just directly copying the OPs For structure but then got annoyed with it. I'll prune it sometime later.
    – b3m2a1
    3 hours ago










  • Excellent answer! I noticed that the "i" outer loop can be further subdivided into n separate smaller loops to utilize the n different cores of my processor and kernels of Mathematica 11.3 for parallel processing. Any idea how to optimize code for this task?
    – dimachaerus
    1 hour ago


















There's no need to put i and j into Module: Do already localizes their names.
– Ruslan
3 hours ago




There's no need to put i and j into Module: Do already localizes their names.
– Ruslan
3 hours ago












@Ruslan ah yeah at first I was just directly copying the OPs For structure but then got annoyed with it. I'll prune it sometime later.
– b3m2a1
3 hours ago




@Ruslan ah yeah at first I was just directly copying the OPs For structure but then got annoyed with it. I'll prune it sometime later.
– b3m2a1
3 hours ago












Excellent answer! I noticed that the "i" outer loop can be further subdivided into n separate smaller loops to utilize the n different cores of my processor and kernels of Mathematica 11.3 for parallel processing. Any idea how to optimize code for this task?
– dimachaerus
1 hour ago






Excellent answer! I noticed that the "i" outer loop can be further subdivided into n separate smaller loops to utilize the n different cores of my processor and kernels of Mathematica 11.3 for parallel processing. Any idea how to optimize code for this task?
– dimachaerus
1 hour ago













1














Initialize the variables



c = 0;
n = 1000000;


Below is a more or less literal translation of the Visual Basic code.



check[i_, j_] := If[(i*i + j*j)/(n*n) < 1, c++]

Timing[
Scan[
Function[i,
Scan[
Function[j,
check[i, j]
],
Range[n]
]
],
Range[n]
]
]


A slightly faster verion gets rid of the overhead caused by the check function and the computation of Range[n] as well as n*n (suggested by dimachaerus comment).



nList = Range[n];
n2 = n*n;

Timing[
Scan[
Function[i,
Scan[
Function[j,
If[(i*i + j*j)/n2 < 1, c++]
],
nList
]
],
nList
]
]


The answers from the two experiments are: (to be inserted when complete)






share|improve this answer























  • Jack, get rid of n * n evaluation each time and instead replace with constant m = n * n
    – dimachaerus
    6 hours ago












  • Ok, but still very very slow compared to VB.
    – dimachaerus
    4 hours ago
















1














Initialize the variables



c = 0;
n = 1000000;


Below is a more or less literal translation of the Visual Basic code.



check[i_, j_] := If[(i*i + j*j)/(n*n) < 1, c++]

Timing[
Scan[
Function[i,
Scan[
Function[j,
check[i, j]
],
Range[n]
]
],
Range[n]
]
]


A slightly faster verion gets rid of the overhead caused by the check function and the computation of Range[n] as well as n*n (suggested by dimachaerus comment).



nList = Range[n];
n2 = n*n;

Timing[
Scan[
Function[i,
Scan[
Function[j,
If[(i*i + j*j)/n2 < 1, c++]
],
nList
]
],
nList
]
]


The answers from the two experiments are: (to be inserted when complete)






share|improve this answer























  • Jack, get rid of n * n evaluation each time and instead replace with constant m = n * n
    – dimachaerus
    6 hours ago












  • Ok, but still very very slow compared to VB.
    – dimachaerus
    4 hours ago














1












1








1






Initialize the variables



c = 0;
n = 1000000;


Below is a more or less literal translation of the Visual Basic code.



check[i_, j_] := If[(i*i + j*j)/(n*n) < 1, c++]

Timing[
Scan[
Function[i,
Scan[
Function[j,
check[i, j]
],
Range[n]
]
],
Range[n]
]
]


A slightly faster verion gets rid of the overhead caused by the check function and the computation of Range[n] as well as n*n (suggested by dimachaerus comment).



nList = Range[n];
n2 = n*n;

Timing[
Scan[
Function[i,
Scan[
Function[j,
If[(i*i + j*j)/n2 < 1, c++]
],
nList
]
],
nList
]
]


The answers from the two experiments are: (to be inserted when complete)






share|improve this answer














Initialize the variables



c = 0;
n = 1000000;


Below is a more or less literal translation of the Visual Basic code.



check[i_, j_] := If[(i*i + j*j)/(n*n) < 1, c++]

Timing[
Scan[
Function[i,
Scan[
Function[j,
check[i, j]
],
Range[n]
]
],
Range[n]
]
]


A slightly faster verion gets rid of the overhead caused by the check function and the computation of Range[n] as well as n*n (suggested by dimachaerus comment).



nList = Range[n];
n2 = n*n;

Timing[
Scan[
Function[i,
Scan[
Function[j,
If[(i*i + j*j)/n2 < 1, c++]
],
nList
]
],
nList
]
]


The answers from the two experiments are: (to be inserted when complete)







share|improve this answer














share|improve this answer



share|improve this answer








edited 6 hours ago

























answered 6 hours ago









Jack LaVigne

11.7k21631




11.7k21631












  • Jack, get rid of n * n evaluation each time and instead replace with constant m = n * n
    – dimachaerus
    6 hours ago












  • Ok, but still very very slow compared to VB.
    – dimachaerus
    4 hours ago


















  • Jack, get rid of n * n evaluation each time and instead replace with constant m = n * n
    – dimachaerus
    6 hours ago












  • Ok, but still very very slow compared to VB.
    – dimachaerus
    4 hours ago
















Jack, get rid of n * n evaluation each time and instead replace with constant m = n * n
– dimachaerus
6 hours ago






Jack, get rid of n * n evaluation each time and instead replace with constant m = n * n
– dimachaerus
6 hours ago














Ok, but still very very slow compared to VB.
– dimachaerus
4 hours ago




Ok, but still very very slow compared to VB.
– dimachaerus
4 hours ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Mathematica Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188423%2fmathematica-code-execution-speed-testing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu