Mathematica is behaving very slow, but it is not like I could compare, as the speed has always been like this. The following ran in 58.64 seconds. Is this normal? How can I speed it up?

   Sum[Sin[(1 - 1/n^2)*2 Pi*t*880] + Sin[(1 - 1/n^2)*2 Pi*t*440] +
     Sin[(1/4 - 1/(n + 1)^2)*2 Pi*t*440], {n, 1, 10}], {t, 0, 5},
   SampleRate -> 44100]]]

Enter image description here

P.S: If the problem is related to computation, I have a pretty powerful GPU; could I get Mathematica to use that?


2 Answers 2


Creating the audio signal as a vector first allows us to use more efficient ways to sample the function:

rate = 44100;
T = 5.;
f[t_] := 
  Sum[Sin[(1 - 1/n^2) 2 Pi t 880] + Sin[(1 - 1/n^2) 2 Pi t 440] + 
    Sin[(1/4 - 1/(n + 1)^2) 2 Pi t 440], {n, 1, 10}];

  a = Audio[Play[f[t], {t, 0., T}, SampleRate -> rate]];

  t = Subdivide[0., T, T rate - 1];
  v = f[t]/(4 Pi);
  b = Audio[v, SampleRate -> rate];



This is more than a 600-fold speed-up. And the two results a and b sound pretty similar. Alas, AudioPlot reveals slight differences. And I did also not get why I have to divide by 4 Pi. I simply guessed this factor by trial and error. I just don't know how Play normalizes the generated signal. Must have to do with how the PlayRange is computed. Maybe anybody else can help?

  • 1
    $\begingroup$ that's a remarkable boost actually! $\endgroup$
    – alex
    Mar 15, 2023 at 12:35
  • 5
    $\begingroup$ Yeah. I think somebody was a bit careless when implementing Play. Would be great if you would send a report on this to Wolfram Support. Thanks. $\endgroup$ Mar 15, 2023 at 15:51

The way you have created the function you are forcing MMA to keep evaluating your expression, which although lightweight on its own, is rather heavy when it needs to be repeated for different values of n and over 5 seconds with a 44 kHz sampling rate.

1. Break things down

You can see that this dramatically reduces your evaluation time.

func = Sum[
   Sin[(1 - 1/n^2)*2 Pi*t*880] + Sin[(1 - 1/n^2)*2 Pi*t*440] + 
    Sin[(1/4 - 1/(n + 1)^2)*2 Pi*t*440], {n, 1, 10}];
AbsoluteTiming[Audio[Play[func, {t, 0, 5}, SampleRate -> 44100]]]

Enter image description here

2. You do not need both Play and Audio

Play[func, {t, 0, 5}, SampleRate -> 44100] // AbsoluteTiming

Enter image description here

You can then export your data as you see it fit.

3. GPU boosting

I am not certain why you would need GPU boosting, but if it is important to use said technology, then you might want to look into Compile and CUDAFunctionLoad.

My advice is that you are complicating your life unnecessarily with those two as they are significantly more advanced uses of Mathematica and you will need to understand far more technical aspects of the language.

Keep in mind that these boostings only offer you an advantage over truly large computations. If your computation time is only 20 seconds, chances are a parallelisable or GPU boosted or compiled function will take longer. That is because setting up the correct kernel distribution is now an added task. Setting up the code to a GPU-readable function will also account for resources. These are better suited for computations that take significantly more time.

Bottom line: anything less than 1 minute is probably not quite worth your while in terms of GPU/parallelisation. (feel free to explore that avenue, of course :) )

  • 1
    $\begingroup$ Thank you I understand now, but a couple of things: Just using play gives me an image which I am not able to play The time for me is still slightly above yours (24 seconds for me and 15 seconds for you), would it because of hardware? And can I somehow get my GPU to produce these? $\endgroup$
    – Anm
    Mar 15, 2023 at 10:14
  • $\begingroup$ The line which contains Play, produces a fully controllable function. You have to click on the Play button (the left facing trangle) which is at the bottom of the output. I am not sure why you want to GPU boost your work. But I've edited my post to give you some directions. $\endgroup$
    – alex
    Mar 15, 2023 at 12:05
  • $\begingroup$ what version of mathematica are you using? $\endgroup$
    – alex
    Mar 15, 2023 at 12:15
  • $\begingroup$ Thanks for the new edit, I will read it soon. I am using Mathematica_13.1.0 where clicking on the buttons does nothing. It just outlines the borders of the widget $\endgroup$
    – Anm
    Mar 15, 2023 at 12:21
  • $\begingroup$ I'm using win10/MMA v.13.2 what OS are you using? $\endgroup$
    – alex
    Mar 15, 2023 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.