# Tag Info

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 ...

22

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 ...

22

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]; ...

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++ ...

19

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 ...

16

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

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

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 ...

13

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] // ...

12

Clear[fa, ga]; fa = Total[{#, # + 1}^2] &; ga = Total[{#, # + 1, # + 2}^2] &; Update: A closed form function soln = Assuming[{C[1] ∈ Integers && C[1] >= 0 && x > 0 && y > 0}, Simplify@ Reduce[Total[{x, x + 1}^2] == Total[{y, y + 1, y + 2}^2], {x, y}, Integers]] /. C[1] -> n; ...

12

For readers who didn't read all the comments, the slowdown is due to a lack of packing of tb, whereas RandomReal returns packed arrays when more than 250 elements are generated. The reason why packing tb fails is because some elements have different precision than others, and (I think?) ToPackedArray requires arrays to be of homogeneous type. To fix this, ...

11

Implementations Here is an "idiomatic" one: ClearAll[mapRec, reverse]; mapRec[f_, ll_] := Block[{\$IterationLimit = Infinity}, reverse@mapRec[{}, f, ll]]; mapRec[accum_, _, {}] := accum; mapRec[accum_, f_, {head_, tail_}] := mapRec[{f[head], accum}, f, tail]; reverse[ll_] := reverse[{}, ll]; reverse[accum_, {}] := accum; reverse[accum_, {head_, tail_}] := ...

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

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 = ...

11

Yves showed you how to shave off a factor of 10, and in the following I'll shave off another factor of 10. First, the original code (plus some data of mine to make it work): points = RandomReal[{0, 10}, {1000, 2}]; n = 500; p = .99; g1 = Graphics[{PointSize[1/n], White, g = Point /@ Join @@ {points, points.{{1, 0}, {0, -1}}}, Array[Rotate[g, Pi ...

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]}, ...

10

Notes: I completely overlooked the fact that rasher specified arbitrary precision input when crafting my functions. Because of this the numeric methods I used will not be as fast. However, to my surprise it seems that at least fn2 is still faster than the FoldList method on arbitrary precision data. Timings with resets on average at about 1 in 40: big = ...

10

One way to save memory is to use the multi-primitive syntax for Point (which should also render quite a bit faster)`: coords = RandomReal[{-1, 1}, {1000, 2}]; Point /@ coords // ByteCount Point@coords // ByteCount %/%% // N (*176200 16552 0.0939387*) For your example you simply drop one character to save a whole lot (about 90%) of kernel real ...

9

A more functional approach: a = With[{f = Subtract @@@ Subsets[Reverse@#, {2}] &}, f[v2]/f[v1]] For a bit more speed you could do this: ii = Join @@ Table[ConstantArray[i, i - 1], {i, n, 2, -1}]; jj = Join @@ Table[Range[j, 1, -1], {j, n - 1, 1, -1}]; a = Divide[Subtract[v2[[ii]], v2[[jj]]], Subtract[v1[[ii]], v1[[jj]]]];

9

You've tried to use Fold, which is good, but the spirit of the algorithm is still very "procedural", in that you're not utilizing the Listable properties of certain functions and you're brute-forcing your way through the set of integers. For instance, Mod can take a list as a second argument, Fold[Times, 1, Range@20] is better written as Times @@ Range@20 ...

9

Here is a simple method that seems to be somewhat faster than yours on unpackable data: colDrop[array_, drop_] := Module[{m = array}, m[[All, drop]] = Sequence[]; m] Test: data = Range /@ RandomInteger[{15, 50}, 500000]; data = Map[FromCharacterCode, data + 37, {2}]; colDropper[data, {1, 3, 5, 8, 10, 11}] // Timing // First colDrop[data, {1, 3, 5, 8, ...

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