Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options not deleted user 731

Questions on optimizing Mathematica code for higher performance. This may mean faster execution, lower memory usage or both. Not to be confused with mathematical optimization.

5 votes
Accepted

How can I speed up the evaluation of a Dynamic Locator acting on a Region?

With RegionNearest RegionNearest is a function that basically processes the region and returns a function of type RegionNearestFunction which is optimized for finding the nearest point in the region …
  • 69.1k
5 votes

How fast can I compare an array of images?

The function that you are looking for is called DistanceMatrix: img = Import["https://i.stack.imgur.com/g6RvB.png"]; parts = ImagePartition[img, ImageDimensions[img]/n]; dm = DistanceMatrix[Flatten[p …
  • 69.1k
4 votes

Speed up image coloring with ReplaceImageValue

You can apply your transformation to a black image of the same dimension to create a mask. When you add this mask to one of your images the black areas of the mask will not affect them, and the white …
  • 69.1k
0 votes

Check if number is in list

I'm guessing (the question should have a small test case so that we can test our understanding of the problem, now it's not really possible to know if we have the right interpretation or not) you need …
  • 69.1k
3 votes

Fastest possible way of moving a line segment through every point on a grid

Some really good methods have already been discussed. Here is another way to write your algorithm, which is much shorter than what you wrote: average[{x_, y_}, grid_] := Module[{sum, dimx, dimy}, { …
  • 69.1k
5 votes
Accepted

How fast can I split an image?

Here is a solution that is a magnitude faster than the fastest method so far when it comes to rendering the image: showParts[img_, n_] := Module[{data, h, w, nrows, ncols, i, j, tmp, new}, data = I …
  • 69.1k
6 votes

Create lists of equivalences from pairs

FixedPoint[Union @@@ Gather[#, IntersectingQ] &, ex1] {{1, 2, 3}, {4, 5}, {6, 7, 8}}
  • 69.1k
7 votes

Fastest way to go from linear index to grid index

Here's what I came up with: getSubindex[index_, stride_] := { Mod[index, stride, 1], Ceiling[index/stride] } getIndex[index_, strides_] := Reverse@FoldPairList[getSubindex, index, Reverse@str …
  • 69.1k
6 votes

How to repeat each element in a list and the whole list as well?

Here's my suggestion: list = {1, 2, 3, 4}; repeat[list_, n_] := PadRight[list, n Length[list], list] repeat[list, 3] {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4} repeat2[list_, n_] := Sequence @@ Cons …
  • 69.1k
15 votes

UnFlatten an array into a ragged matrix

MapThread[ Partition, {Internal`PartitionRagged[myflatarray, Times @@@ mydimensions], mydimensions[[All, 2]]} ] Internal`PartitionRagged is undocumented. An example of how it works is: Internal` …
  • 69.1k
14 votes
Accepted

Speed and the MapThread function

This is because of the compilation that kicks in automatically if the list in Map exceeds a certain number of elements. "MapCompileLength" /. ("CompileOptions" /. SystemOptions["CompileOptions"]) (* …
  • 69.1k
11 votes
Accepted

Finding number of pixels with certain RGB color in an image

ImageLevels[ ColorConvert[img, "Grayscale"] ][[1, 2]] // RepeatedTiming (* Out: {0.0029, 144316} *) Generally, for speed, you want to avoid pattern matching altogether.
  • 69.1k
13 votes
Accepted

How to check whether a sublist exist in a huge database lists in a fast way?

Cases is pretty fast. Consider the case where you have 10,000 lists, each with 100 numbers. Cases can find all lists with the given subsequence in less than 0.05 seconds: SeedRandom[100] data = Rando …
  • 69.1k
10 votes

Making a faster alternative for {PatternSequence[1, PatternSequence[2, 3 ..] ..] ..}

This solution tries to reduce the list into a list of a single type of elements, if it succeeds then the list is following the prescribed pattern. MatchQ[ SequenceReplace[ SequenceReplace[list, { …
  • 69.1k
1 vote
Accepted

Fast value search and replacement in a matrix with expansion for multiple values

I propose the following: update[rules_][list_] := With[{res = ReplaceList[list, rules]}, If[res == {}, {list}, res] ] rules = Dispatch[{a : Repeated[_, {2}], #, b : Repeated[_, {2}]} :> …
  • 69.1k

15 30 50 per page