# Tag Info

## Hot answers tagged performance-tuning

34

New version update The July 29, 2014 version of Mathematica brings to the RPi the ability to evaluate expressions on the cloud, in other words, to cheat at benchmarking! As an added bonus, we have a default retro color scheme for BarCharts that is sure to please fans of the 70s. I have a sudden urge to dust off my Algol and Cobol handbooks. Ahem. The ...

28

This is a general guide on debugging issues with parallelization performance. 1. Measuring performance The proper way to measure the timing of parallelized calculations is AbsoluteTiming, which measures wall time. Timing measures CPU time on the main kernel only and won't give a correct result when used with parallel calculations. 2. How to parallelize ...

28

Well, you can use the undocumented RegionDistance which does exactly this as follows: (This answer, as written, only works for V9 as noted by Oska, for V10 see update below) here is a triangle in 3D region = Polygon[{{0, 0, 0}, {1, 0, 0}, {0, 1, 1}}]; Graphics3D[region] Now suppose you want to find the shortest distance from the point {1, 1, 1} in 3D ...

21

As @RahulNarain says, forming the image point by point saves significant memory because the number of image pixels is typically much smaller than the hundreds of millions of iterations that compose it. Therefore, iterate the attractor equations, and for each point generated, find its location within the image matrix. Colour coding of the number of hits in ...

19

@Simon Woods points out in a comment that: In fact the delay on the initial run is caused by compiling code to provide the Poisson distribution :-) You can look at ImageColorOperationsDumpiImageEffectPoissonNoise to see how it works internally. Now, although PoissonDistribution can't be compiled, there's nothing stopping the use of my own C++ ...

18

Diagnosis Spelunking the definition of Commonest, which is written in top-level Mathematica code, I see that the two parameter form is handled by this internal function: Commonest; (* preload *) ? StatisticsDescriptiveDumpoCommonestSetLength oCommonestSetLength[list_, n_] := Catch[Block[{res, reslen, ord}, res = Tally[list]; reslen = Length[res]; ...

17

Here's a faster way: Clear[f] Timing[MapIndexed[If[Not@IntegerQ@f[#1], f[#1] = First[#2]] &, list];] Now f[elem] will tell you the position of the first occurrence of elem. On my machine this is approximately 8-10 times faster than your approach for a list of 10000 elements. The timing for a=5000, b=100 is 1.3 s on my machine. In general I expect ...

16

Well, you can trade memory for speed and use Compile, as follows: accumC = Compile[{{l, _Integer, 1}, {max, _Integer}}, Module[{accum = Table[0, {max}], res = Table[0, {Length[l]}]}, Do[res[[i]] = ++accum[[l[[i]]]], {i, Length[l]}]; res ] ] ClearAll[occurrences]; occurrences[lst_List] := With[{rules = Thread[# -> ...

15

I have found that my approach with textures has different applications: How to plot contours in the faces of a cube? How to plot ternary density plots? Now I want to use it for the enhancement of the DensityPlot: Options[fastDensityPlot] = Append[Options[DensityPlot], Subpoints -> 30]; SyntaxInformation[fastDensityPlot] = ...

15

From Oleksandr's comments: From my limited experience of Geekbench, the odd choice of weightings for the different components makes the result rather meaningless as a practical performance metric. This is a general problem: any real workload emphasizes particular aspects of performance more than others, and unless you can define the workload very ...

14

UPDATE I thought it would be neat to try and animate the thing, so I let the $a$ parameter run between $-\pi$ and $\pi$. I generated 600 images and put them together using ffmpeg. Check it out on youtube. It might not be in the spirit of Mathematica Stack Exchange, but allow me an objection - stuff that is slow in Mathematica should be kept out of it. To ...

14

You can just step through $i$ and $j$ while trying to simultaneously satisfy: $$i^2+(i+1)^2=j^2+(j+1)^2+(j+2)^2$$ Just loop and if the inequality is too small on the left, increment $i$. If it's too small on the right, increment $j$. That looks like this: Clear[f, g, i, j]; f[i_] = i^2 + (i + 1)^2; g[j_] = j^2 + (j + 1)^2 + (j + 2)^2; max = 10^6; i = 1; j ...

14

Your test is quite synthetic: you take only few first elements. If you you have longer sequence of positive elements then build-in LengthWhile is faster lst = RandomInteger[{-1, 30000}, 100000]; rst1 = LengthWhile[lst, # >= 0 &]; // AbsoluteTiming rst2 = lengthwhile[lst, # >= 0 &]; // AbsoluteTiming rst1 == rst2 (* {0.096340, Null} *) (* ...

14

There are several reasons. Firstly the built-in function has some minor overhead to check the arguments and call the appropriate internal function depending on whether the first argument is a list, a sparse array or an association. Secondly, with a packed array, LengthWhile uses compilation in an attempt to increase performance. There is some overhead in ...

13

Nasser gives good standard ControlActive approach. But that by definition looses quality during motion. I just would like to share a trick that avoids that. Most of the time is spent on rendering your bell shape. But it is static. Plane moves but it is simple, so it should not all the time trigger recomputing of static bell shape. You can separate motion of ...

13

Let's start by taking a look at the compiled form of one of our queries: DatasetCompileQuery[Query @ First @ spans] (* DatasetWithOverrides@*Checked[Slice[205 ;; 313], Identity] *) We can see that the operation is not implemented directly in terms of part. Indeed, there are three components: DatasetWithOverrides, GeneralUtilitiesChecked and ...

13

For unknown reasons, TensorProduct produces unpacked array (see packed arrays here). You can use Outer[Times, a, b] instead: $HistoryLength = 0; L = 2000; a = RandomReal[{-10^6, 10^6}, {L, L}]; b = RandomReal[{-10^6, 10^6}, 3]; c = TensorProduct[a, b]; c2 = Outer[Times, a, b]; d = RandomReal[{-10^6, 10^6}, {L, L, 3}]; ByteCount /@ {a, b, c, c2, d} // Column ... 13 Here is a solution based on binary search (compiled). Implementation First, this is a version of a binary search, which would return the position of a maximal number in a list, smaller or equal to yours, and -1 if no such is found: ClearAll[bsminComp]; bsminComp= Compile[ {{lst,_Integer,1},{elem,_Integer}}, ... 13 To create ExperimentalNumericalFunction, one needs to evaluate ExperimentalCreateNumericalFunction[vars, expr, dims] where vars is a list of arguments, expr - the expression from which the numerical function will be created, dims - the dimensions of the output matrix produced by this expression. If the output is scalar, then dims should be set to {}. It ... 13 One of the main bottlenecks in your code are the texture you apply on each surface. Try to use texture = {}; in your drawTile function and the graphics should be faster by magnitudes. Additionally, as Yves mentioned, Tubes are a performance killer too. Therefore, a workaround for your problem might be to Rasterize the texture graphics by yourself and use ... 13 I believe that this is an intentional and beneficial change in v10. Mathematica 9 was not able to correctly detect the number of physical cores, and it launched as many kernels as the number of virtual cores (which is double when using HyperThreading). Mathematica 10 can now detect the number of physical cores correctly and will launch only as many kernels ... 12 This is not a question of functional style per se. Functional style is, AFAICT, largely about avoiding mutable state (where possible), using functions as fundamental building blocks, using certain abstractions such as higher-order functions, closures, function composition, etc., and avoiding mixing state and behavior. I think that the value of pre ... 11 The only improvement I can think of: comm[a_, n_] := #[[ Ordering[#[[All, 2]], -n] ]] & @ Tally[a] Or if you don't want the counts: comm2[a_, n_] := #[[ Ordering[#2, -n] ]] & @@ (Tally[a]\[Transpose]) Test: MyCommonest2[a, 15] // Timing // First comm[a, 15] // Timing // First comm2[a, 15] // Timing // First 0.3276 0.3056 ... 11 This seems fast(er): Extract[a, Transpose[{v, Range@Length@v}]] Addendum Mr.Wizard's clean method Diagonal @ a[[v]] has a surprising property for those of us who think that packed arrays rank just below the wheel in the list of inventions for the sake of efficiency. For unpacked arrays a, it uses virtually no extra memory. Example Initialization. ... 11 Maybe upperTriangularMatrixQ2[mat_?MatrixQ] /; Equal @@ Dimensions@mat := UpperTriangularize@mat == mat; test = RandomInteger[{1, 100}, {1000, 1000}]; upperTriangularMatrixQ@test // AbsoluteTiming {2.126050, False} upperTriangularMatrixQ2@test // AbsoluteTiming {0.003277, False} test2 = UpperTriangularize@test; upperTriangularMatrixQ@test2 ... 11 Update: the lhs of$i^2+(i+1)^2=j^2+(j+1)^2+(j+2)^2$is always odd so$j\$ should be even. The fastest solution I found: max = 10^12; {(Sqrt[3 + 6 (# + 1)^2] - 1)/2, #, #^2 + (# + 1)^2 + (# + 2)^2} & @@@ (2 Position[#, 0]) &@UnitStep[Abs[# - Round[#]] - 1.*^-10] &[ Sqrt[0.75 + 1.5 #^2] - 0.5] &@ Range[3., Sqrt[max/3.], 2] // ...

11

Okay, this is a bit of an embarassment. Here is a very small modification of the original code. I simply made explicit option settings, made a denominator to Sin explicitly real, that kind of thing. My tests show the same timing as the original, give or take an iota. ie = 200; ez = ConstantArray[0., {ie + 1}]; hy = ConstantArray[0., {ie}]; fdtd1d = ...

10

This is what I do. I have been using this method for long time. The idea is simple. Use the second argument of Dynamics. In there, make any changes to the state of the program you want, that only relates to the change of the current control variable being changed. In your case, in the second argument of a and b, you can make your heavy computations. In t, ...

10

I think that c[_] = 0; ++c[#] & /@ list is a perfectly good method and one that I make use of myself. There is an advantage in the fact that it can be interrupted or continued at any time, and it keeps a running count of the elements in the DownValues of c. (If not using Module that is.) Therefore I think it is a nice general method. One may ...

10

Your main inefficiency seems to be in actually extracting the found strings, not in finding the positions. Here is a version based on ToCharacterCode - FromCharacterCode, which is quite a bit faster: ClearAll[extractAll]; extractAll[s_String,startpos_List,size_Integer?Positive]:= With[{codes=ToCharacterCode[s]}, With[{len=Length[codes]}, ...

Only top voted, non community-wiki answers of a minimum length are eligible