How to speed up a processAlternatives to procedural loops and iterating over lists in MathematicaWhy should I...
Reason Why Dimensional Travelling Would be Restricted
Exponential growth/decay formula: what happened to the other constant of integration?
Can you use a beast's innate abilities while polymorphed?
What's the difference between a cart and a wagon?
Compare four integers, return word based on maximum
Can I become debt free or should I file for bankruptcy? How do I manage my debt and finances?
Is there any relevance to Thor getting his hair cut other than comedic value?
Find the next monthly expiration date
I am on the US no-fly list. What can I do in order to be allowed on flights which go through US airspace?
What is better: yes / no radio, or simple checkbox?
Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?
Borrowing Characters
Is there a low-level alternative to Animate Objects?
How can I be pwned if I'm not registered on that site?
What do the pedals on grand pianos do?
What can I substitute for soda pop in a sweet pork recipe?
Make me a metasequence
Linear regression when Y is bounded and discrete
Does music exist in Panem? And if so, what kinds of music?
Significance and timing of "mux scans"
What is a term for a function that when called repeatedly, has the same effect as calling once?
How to speed up a process
Is my plan for fixing my water heater leak bad?
Is it 40% or 0.4%?
How to speed up a process
Alternatives to procedural loops and iterating over lists in MathematicaWhy should I avoid the For loop in Mathematica?Getting lengths of sublists that sum to more than oneHow to speed up multi-curve plotting?Speed up replacement using Rule Dispatch AssocitationReflecting lines in ellipses: speed up the code?Why is CurrencyConvert so slow?Performance of a bootstrap - time of computation not linear with number of iterationsHow to speed up computation of medoids?How to speed up plotting with PlotPoints?How to speed up ListPlot when each point has its own colorIncresing efficiency of the code:
$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 7 hours ago
LittleFingerLittleFinger
864
864
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
Forloops: see: Why should I avoid the For loop in Mathematica?
- avoid
Append/AppendTobecause they generate a new list every time you add an element; instead, generate lists withTable,Array,Range,Reap / Sowinstead; - 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 theForloop.
$endgroup$
– mikado
5 hours ago
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. / Pithen, to avoidNand squeeze the last few microseconds out
$endgroup$
– MarcoB
6 hours ago
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 6 hours ago
MikeYMikeY
3,208514
3,208514
add a comment |
add a comment |
$begingroup$
Total@Range[11111]/Pi // N
(*Out: 1.96501*10^7 *)
In general:
- avoid
Forloops: see: Why should I avoid the For loop in Mathematica?
- avoid
Append/AppendTobecause they generate a new list every time you add an element; instead, generate lists withTable,Array,Range,Reap / Sowinstead; - 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 theForloop.
$endgroup$
– mikado
5 hours ago
add a comment |
$begingroup$
Total@Range[11111]/Pi // N
(*Out: 1.96501*10^7 *)
In general:
- avoid
Forloops: see: Why should I avoid the For loop in Mathematica?
- avoid
Append/AppendTobecause they generate a new list every time you add an element; instead, generate lists withTable,Array,Range,Reap / Sowinstead; - 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 theForloop.
$endgroup$
– mikado
5 hours ago
add a comment |
$begingroup$
Total@Range[11111]/Pi // N
(*Out: 1.96501*10^7 *)
In general:
- avoid
Forloops: see: Why should I avoid the For loop in Mathematica?
- avoid
Append/AppendTobecause they generate a new list every time you add an element; instead, generate lists withTable,Array,Range,Reap / Sowinstead; - 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
Forloops: see: Why should I avoid the For loop in Mathematica?
- avoid
Append/AppendTobecause they generate a new list every time you add an element; instead, generate lists withTable,Array,Range,Reap / Sowinstead; - 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 5 hours ago
answered 7 hours ago
MarcoBMarcoB
36.7k556112
36.7k556112
$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 theForloop.
$endgroup$
– mikado
5 hours ago
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 theForloop.
$endgroup$
– mikado
5 hours ago
$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
5 hours ago
$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
5 hours ago
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. / Pithen, to avoidNand squeeze the last few microseconds out
$endgroup$
– MarcoB
6 hours ago
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. / Pithen, to avoidNand squeeze the last few microseconds out
$endgroup$
– MarcoB
6 hours ago
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 6 hours ago
answered 6 hours ago
Henrik SchumacherHenrik Schumacher
55.4k576154
55.4k576154
1
$begingroup$
He. Good old Carl Friedrich to the rescue... :-) I'd go forn (n + 1)/2. / Pithen, to avoidNand squeeze the last few microseconds out
$endgroup$
– MarcoB
6 hours ago
add a comment |
1
$begingroup$
He. Good old Carl Friedrich to the rescue... :-) I'd go forn (n + 1)/2. / Pithen, to avoidNand squeeze the last few microseconds out
$endgroup$
– MarcoB
6 hours ago
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
6 hours ago
$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
6 hours ago
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