How to speed up a process
$begingroup$
I am wondering how to speed up the following code: Currently it takes more than two sec to be processed.
Timing[For[i = 1;
list = {}, i <= 11111, i++, l = AppendTo[list, i/Pi]];
Total@N@list]
I am new to Wolfram languages and it's the only thing I could come up with.
Any idea ?
Thanks
performance-tuning timing
$endgroup$
add a comment |
$begingroup$
I am wondering how to speed up the following code: Currently it takes more than two sec to be processed.
Timing[For[i = 1;
list = {}, i <= 11111, i++, l = AppendTo[list, i/Pi]];
Total@N@list]
I am new to Wolfram languages and it's the only thing I could come up with.
Any idea ?
Thanks
performance-tuning timing
$endgroup$
add a comment |
$begingroup$
I am wondering how to speed up the following code: Currently it takes more than two sec to be processed.
Timing[For[i = 1;
list = {}, i <= 11111, i++, l = AppendTo[list, i/Pi]];
Total@N@list]
I am new to Wolfram languages and it's the only thing I could come up with.
Any idea ?
Thanks
performance-tuning timing
$endgroup$
I am wondering how to speed up the following code: Currently it takes more than two sec to be processed.
Timing[For[i = 1;
list = {}, i <= 11111, i++, l = AppendTo[list, i/Pi]];
Total@N@list]
I am new to Wolfram languages and it's the only thing I could come up with.
Any idea ?
Thanks
performance-tuning timing
performance-tuning timing
asked yesterday
user63037
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
You can see in @MarcoB's answer the enormous speed up possible.
Even for your code, if you replace the AppendTo
with a different structure, you can get orders of magnitude improvement.
Your code here - note that we don't need l
for anything.
( For[i = 1; list = {}, i <= 11111, i++, AppendTo[list, i/Pi]];
Total@N@list ) //AbsoluteTiming
{2.12476, 1.96501*10^7}
Slight modification results in factor of 100 speed up.
(For[i = 1; list = {}, i <= 11111, i++, list = {list, i/Pi}];
Total@N@Flatten@list) // AbsoluteTiming
{0.0287278, 1.96501*10^7}
$endgroup$
add a comment |
$begingroup$
Total@Range[11111]/Pi // N
(*Out: 1.96501*10^7 *)
In general:
- avoid
For
loops: see: Why should I avoid the For loop in Mathematica?
- avoid
Append
/AppendTo
because they generate a new list every time you add an element; instead, generate lists withTable
,Array
,Range
,Reap / Sow
instead; - See also: Alternatives to procedural loops and iterating over lists in Mathematica
Just for some timing context, and to compare For
with Do
:
n = 10^6; rpt = RepeatedTiming;
(For[i = 1; list = {}, i <= n, i++, list = {list, i}]; Total@N@Flatten[list];) // rpt
(For[i = 1; list = {}, i <= n, i++, list = {list, i}]; N@Total@Flatten[list];) // rpt
(list = {}; Do[list = {list, i}, {i, n}]; Total@N@Flatten[list];) // rpt
(list = {}; Do[list = {list, i}, {i, n}]; N@Total@Flatten[list];) // rpt
Total@N@Range[n]; // rpt
N@Total@Range[n]; // rpt
n (n + 1)/2.; // rpt
(* Out:
For loops: 1.35 s
1.40 s
Do loops: 0.980 s
0.887 s
Range: 0.01 s
0.007 s
formula: 1 x 10^-6 s
*)
$endgroup$
$begingroup$
Of course, you don't need to know the formula. Mathematica will work it out for you in fraction of the time it took to do theFor
loop.
$endgroup$
– mikado
yesterday
add a comment |
$begingroup$
n = 11111;
Total@Range[11111]/Pi // N // RepeatedTiming
n (n + 1)/(2. Pi)// RepeatedTiming
{0.000034, 1.96501*10^7}
{1.2*10^-6, 1.96501*10^7}
$endgroup$
1
$begingroup$
He. Good old Carl Friedrich to the rescue... :-) I'd go forn (n + 1)/2. / Pi
then, to avoidN
and squeeze the last few microseconds out
$endgroup$
– MarcoB
yesterday
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f192603%2fhow-to-speed-up-a-process%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
You can see in @MarcoB's answer the enormous speed up possible.
Even for your code, if you replace the AppendTo
with a different structure, you can get orders of magnitude improvement.
Your code here - note that we don't need l
for anything.
( For[i = 1; list = {}, i <= 11111, i++, AppendTo[list, i/Pi]];
Total@N@list ) //AbsoluteTiming
{2.12476, 1.96501*10^7}
Slight modification results in factor of 100 speed up.
(For[i = 1; list = {}, i <= 11111, i++, list = {list, i/Pi}];
Total@N@Flatten@list) // AbsoluteTiming
{0.0287278, 1.96501*10^7}
$endgroup$
add a comment |
$begingroup$
You can see in @MarcoB's answer the enormous speed up possible.
Even for your code, if you replace the AppendTo
with a different structure, you can get orders of magnitude improvement.
Your code here - note that we don't need l
for anything.
( For[i = 1; list = {}, i <= 11111, i++, AppendTo[list, i/Pi]];
Total@N@list ) //AbsoluteTiming
{2.12476, 1.96501*10^7}
Slight modification results in factor of 100 speed up.
(For[i = 1; list = {}, i <= 11111, i++, list = {list, i/Pi}];
Total@N@Flatten@list) // AbsoluteTiming
{0.0287278, 1.96501*10^7}
$endgroup$
add a comment |
$begingroup$
You can see in @MarcoB's answer the enormous speed up possible.
Even for your code, if you replace the AppendTo
with a different structure, you can get orders of magnitude improvement.
Your code here - note that we don't need l
for anything.
( For[i = 1; list = {}, i <= 11111, i++, AppendTo[list, i/Pi]];
Total@N@list ) //AbsoluteTiming
{2.12476, 1.96501*10^7}
Slight modification results in factor of 100 speed up.
(For[i = 1; list = {}, i <= 11111, i++, list = {list, i/Pi}];
Total@N@Flatten@list) // AbsoluteTiming
{0.0287278, 1.96501*10^7}
$endgroup$
You can see in @MarcoB's answer the enormous speed up possible.
Even for your code, if you replace the AppendTo
with a different structure, you can get orders of magnitude improvement.
Your code here - note that we don't need l
for anything.
( For[i = 1; list = {}, i <= 11111, i++, AppendTo[list, i/Pi]];
Total@N@list ) //AbsoluteTiming
{2.12476, 1.96501*10^7}
Slight modification results in factor of 100 speed up.
(For[i = 1; list = {}, i <= 11111, i++, list = {list, i/Pi}];
Total@N@Flatten@list) // AbsoluteTiming
{0.0287278, 1.96501*10^7}
answered yesterday
MikeYMikeY
3,248614
3,248614
add a comment |
add a comment |
$begingroup$
Total@Range[11111]/Pi // N
(*Out: 1.96501*10^7 *)
In general:
- avoid
For
loops: see: Why should I avoid the For loop in Mathematica?
- avoid
Append
/AppendTo
because they generate a new list every time you add an element; instead, generate lists withTable
,Array
,Range
,Reap / Sow
instead; - See also: Alternatives to procedural loops and iterating over lists in Mathematica
Just for some timing context, and to compare For
with Do
:
n = 10^6; rpt = RepeatedTiming;
(For[i = 1; list = {}, i <= n, i++, list = {list, i}]; Total@N@Flatten[list];) // rpt
(For[i = 1; list = {}, i <= n, i++, list = {list, i}]; N@Total@Flatten[list];) // rpt
(list = {}; Do[list = {list, i}, {i, n}]; Total@N@Flatten[list];) // rpt
(list = {}; Do[list = {list, i}, {i, n}]; N@Total@Flatten[list];) // rpt
Total@N@Range[n]; // rpt
N@Total@Range[n]; // rpt
n (n + 1)/2.; // rpt
(* Out:
For loops: 1.35 s
1.40 s
Do loops: 0.980 s
0.887 s
Range: 0.01 s
0.007 s
formula: 1 x 10^-6 s
*)
$endgroup$
$begingroup$
Of course, you don't need to know the formula. Mathematica will work it out for you in fraction of the time it took to do theFor
loop.
$endgroup$
– mikado
yesterday
add a comment |
$begingroup$
Total@Range[11111]/Pi // N
(*Out: 1.96501*10^7 *)
In general:
- avoid
For
loops: see: Why should I avoid the For loop in Mathematica?
- avoid
Append
/AppendTo
because they generate a new list every time you add an element; instead, generate lists withTable
,Array
,Range
,Reap / Sow
instead; - See also: Alternatives to procedural loops and iterating over lists in Mathematica
Just for some timing context, and to compare For
with Do
:
n = 10^6; rpt = RepeatedTiming;
(For[i = 1; list = {}, i <= n, i++, list = {list, i}]; Total@N@Flatten[list];) // rpt
(For[i = 1; list = {}, i <= n, i++, list = {list, i}]; N@Total@Flatten[list];) // rpt
(list = {}; Do[list = {list, i}, {i, n}]; Total@N@Flatten[list];) // rpt
(list = {}; Do[list = {list, i}, {i, n}]; N@Total@Flatten[list];) // rpt
Total@N@Range[n]; // rpt
N@Total@Range[n]; // rpt
n (n + 1)/2.; // rpt
(* Out:
For loops: 1.35 s
1.40 s
Do loops: 0.980 s
0.887 s
Range: 0.01 s
0.007 s
formula: 1 x 10^-6 s
*)
$endgroup$
$begingroup$
Of course, you don't need to know the formula. Mathematica will work it out for you in fraction of the time it took to do theFor
loop.
$endgroup$
– mikado
yesterday
add a comment |
$begingroup$
Total@Range[11111]/Pi // N
(*Out: 1.96501*10^7 *)
In general:
- avoid
For
loops: see: Why should I avoid the For loop in Mathematica?
- avoid
Append
/AppendTo
because they generate a new list every time you add an element; instead, generate lists withTable
,Array
,Range
,Reap / Sow
instead; - See also: Alternatives to procedural loops and iterating over lists in Mathematica
Just for some timing context, and to compare For
with Do
:
n = 10^6; rpt = RepeatedTiming;
(For[i = 1; list = {}, i <= n, i++, list = {list, i}]; Total@N@Flatten[list];) // rpt
(For[i = 1; list = {}, i <= n, i++, list = {list, i}]; N@Total@Flatten[list];) // rpt
(list = {}; Do[list = {list, i}, {i, n}]; Total@N@Flatten[list];) // rpt
(list = {}; Do[list = {list, i}, {i, n}]; N@Total@Flatten[list];) // rpt
Total@N@Range[n]; // rpt
N@Total@Range[n]; // rpt
n (n + 1)/2.; // rpt
(* Out:
For loops: 1.35 s
1.40 s
Do loops: 0.980 s
0.887 s
Range: 0.01 s
0.007 s
formula: 1 x 10^-6 s
*)
$endgroup$
Total@Range[11111]/Pi // N
(*Out: 1.96501*10^7 *)
In general:
- avoid
For
loops: see: Why should I avoid the For loop in Mathematica?
- avoid
Append
/AppendTo
because they generate a new list every time you add an element; instead, generate lists withTable
,Array
,Range
,Reap / Sow
instead; - See also: Alternatives to procedural loops and iterating over lists in Mathematica
Just for some timing context, and to compare For
with Do
:
n = 10^6; rpt = RepeatedTiming;
(For[i = 1; list = {}, i <= n, i++, list = {list, i}]; Total@N@Flatten[list];) // rpt
(For[i = 1; list = {}, i <= n, i++, list = {list, i}]; N@Total@Flatten[list];) // rpt
(list = {}; Do[list = {list, i}, {i, n}]; Total@N@Flatten[list];) // rpt
(list = {}; Do[list = {list, i}, {i, n}]; N@Total@Flatten[list];) // rpt
Total@N@Range[n]; // rpt
N@Total@Range[n]; // rpt
n (n + 1)/2.; // rpt
(* Out:
For loops: 1.35 s
1.40 s
Do loops: 0.980 s
0.887 s
Range: 0.01 s
0.007 s
formula: 1 x 10^-6 s
*)
edited yesterday
answered yesterday
MarcoBMarcoB
36.7k556113
36.7k556113
$begingroup$
Of course, you don't need to know the formula. Mathematica will work it out for you in fraction of the time it took to do theFor
loop.
$endgroup$
– mikado
yesterday
add a comment |
$begingroup$
Of course, you don't need to know the formula. Mathematica will work it out for you in fraction of the time it took to do theFor
loop.
$endgroup$
– mikado
yesterday
$begingroup$
Of course, you don't need to know the formula. Mathematica will work it out for you in fraction of the time it took to do the
For
loop.$endgroup$
– mikado
yesterday
$begingroup$
Of course, you don't need to know the formula. Mathematica will work it out for you in fraction of the time it took to do the
For
loop.$endgroup$
– mikado
yesterday
add a comment |
$begingroup$
n = 11111;
Total@Range[11111]/Pi // N // RepeatedTiming
n (n + 1)/(2. Pi)// RepeatedTiming
{0.000034, 1.96501*10^7}
{1.2*10^-6, 1.96501*10^7}
$endgroup$
1
$begingroup$
He. Good old Carl Friedrich to the rescue... :-) I'd go forn (n + 1)/2. / Pi
then, to avoidN
and squeeze the last few microseconds out
$endgroup$
– MarcoB
yesterday
add a comment |
$begingroup$
n = 11111;
Total@Range[11111]/Pi // N // RepeatedTiming
n (n + 1)/(2. Pi)// RepeatedTiming
{0.000034, 1.96501*10^7}
{1.2*10^-6, 1.96501*10^7}
$endgroup$
1
$begingroup$
He. Good old Carl Friedrich to the rescue... :-) I'd go forn (n + 1)/2. / Pi
then, to avoidN
and squeeze the last few microseconds out
$endgroup$
– MarcoB
yesterday
add a comment |
$begingroup$
n = 11111;
Total@Range[11111]/Pi // N // RepeatedTiming
n (n + 1)/(2. Pi)// RepeatedTiming
{0.000034, 1.96501*10^7}
{1.2*10^-6, 1.96501*10^7}
$endgroup$
n = 11111;
Total@Range[11111]/Pi // N // RepeatedTiming
n (n + 1)/(2. Pi)// RepeatedTiming
{0.000034, 1.96501*10^7}
{1.2*10^-6, 1.96501*10^7}
edited yesterday
answered yesterday
Henrik SchumacherHenrik Schumacher
55.4k576154
55.4k576154
1
$begingroup$
He. Good old Carl Friedrich to the rescue... :-) I'd go forn (n + 1)/2. / Pi
then, to avoidN
and squeeze the last few microseconds out
$endgroup$
– MarcoB
yesterday
add a comment |
1
$begingroup$
He. Good old Carl Friedrich to the rescue... :-) I'd go forn (n + 1)/2. / Pi
then, to avoidN
and squeeze the last few microseconds out
$endgroup$
– MarcoB
yesterday
1
1
$begingroup$
He. Good old Carl Friedrich to the rescue... :-) I'd go for
n (n + 1)/2. / Pi
then, to avoid N
and squeeze the last few microseconds out$endgroup$
– MarcoB
yesterday
$begingroup$
He. Good old Carl Friedrich to the rescue... :-) I'd go for
n (n + 1)/2. / Pi
then, to avoid N
and squeeze the last few microseconds out$endgroup$
– MarcoB
yesterday
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f192603%2fhow-to-speed-up-a-process%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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