Questions about optimizing code for faster results, or how to do faster computations.

learn more… | top users | synonyms

4
votes
2answers
355 views

Colorize Grayscale Image

I have a grayscale image that contains two objects. The grayscale pixel values can be divided into three intervals where two of them represent objects and the third represents the background. The ...
2
votes
1answer
108 views

2D Fourier transform of a few (4) disjoint discs on a plane

I'd really appreciate some advice. Short Version I'm trying to calculate the following $$ ...
6
votes
1answer
98 views

Collision detection algorithm (discrete) with sweep and prune algorithm

For a more complex collision detection approach, I need an efficient (aka fast) algorithm to prune the elements by using boundary boxes. A classical algorithm is the so called sweep and prune (SAP) ...
3
votes
3answers
247 views

Faster Integer Approximation of pi

I'd like to speed up this calculation: First[Sort[Table[{k,Abs[Round[k/Pi]Pi - k]1.{k,1,100000}],#1[[2]]<#2[[2]] &]] The code above generates a list of ...
20
votes
3answers
1k views

Maple vs. Mathematica

I am reading the document "How Maple Compares to Mathematica" found at http://www.maplesoft.com/products/maple/compare/HowMapleComparestoMathematica.pdf. On page 15 there is an example where ...
1
vote
1answer
74 views

NSolve for complicated equation involving double cross product

I have the following issue. I am trying to solve a 3-vector equation for the vector slx, sly, and ...
6
votes
3answers
207 views

Speed of ConvexHullMesh

Pick $N$ random points on the surface of the sphere, then use ConvexHullMesh[] to compute their convex hull. Empirically, this takes time quadratic time in $N,$ ...
9
votes
4answers
275 views

Replacing matrix values

I want to find the n largest values in a matrix and replace all others with zero. The solution I found uses ReplaceAll and becomes very slow as the size of ...
9
votes
2answers
162 views

Extracting values with multiple keys

I want to extract sorted data from a huge database based upon two (or more) keys in a very timely manner. Here is a reproducible toy example for two keys only: ...
17
votes
5answers
1k views

How to speed up my Project Euler code

The evaluation speed of Mathematica often depresses me. It did it again when I wrote a code to solve a Project Euler problem. https://projecteuler.net/problem=206 Here's my code: ...
5
votes
4answers
236 views

Speed up Schwarz-Christoffel mapping

I would like to ask your advice how to speed up Contour and Parametric plots in the following example. Let as start by defining a function ...
0
votes
1answer
48 views

Speeding up ReplaceRepeated while truncating to desired order

I need to program in an algorithm that recursively makes algebraic replacements which leads to an utterly complicated algebraic function of $x$, but whose final result is only needed at fixed order in ...
2
votes
0answers
75 views

An efficient way to merge a pair of 4d arrays of non regular length lists

Question I have two 4 dimensional non regular length lists of complex values. I have working code however it seems quite slow and I am not sure how I could improve this. Mathematically I am trying ...
7
votes
1answer
116 views

How do I speed up a plotting of NIntegrate when repeated multiple times inside Plot?

I am studying a set of functions (many of which I know only as a definite integral) and I have assembled into a list. Here is a sample: ...
8
votes
3answers
208 views

Slow work of SQLInsert with SQLite

I want to store my data in SQLite database, but writing large numbers of data its too slow. For example: ...
2
votes
2answers
75 views

Improving the speed/efficiency of multiple (finite range) convolutions for a causal response plot

So I am looking at modelling the response of a system that is excited by multiple pulses over a period of time. The way to find the response at time t is to take the convolution between the impulse ...
4
votes
2answers
188 views

Time-efficient creation of matrix

I have expressions like xx1=FF[1, 1] GG[1, 1] + FF[1, 1] GG[2, 2] + FF[2, 2] GG[2, 2] xx2=2*FF[1, 2] GG[1, 1] + FF[1, 1] GG[1, 2] + FF[2, 2] GG[2, 2] and I want ...
3
votes
2answers
108 views

Time-efficient manipulation (zeroing) of expression

I have huge matrices in the form of ...
2
votes
1answer
119 views

How can I reduce computation time while still obtaining a good approximation for my function?

I am new to any CAS (and Mathematica, for that matter) and new to StackExchange too, so forgive me and correct me on any mistakes. I have this function: $J_p=\sum_{m,n=1}^{\infty} ...
21
votes
2answers
450 views

Why is Query so much slower than Part?

Time for another of these(1),(2) as yet another new-in-10 function appears to have poor performance compared to older alternatives. This time: Query appears to be ...
3
votes
1answer
124 views

Why is my compiled code slower?

I am trying to make a Mathematica program run faster. Here is part of the original version. It takes as input the integers X (of order 100), J (of order 10000), and n (of order 100), the real number p ...
15
votes
1answer
272 views

Speed up adding Poisson noise to an image

I'm simulating some images corrupted with Poisson noise, but I'm encountering a few problems with performance. According to the documentation on ImageEffect, one ...
5
votes
1answer
146 views

Is there a faster way to Map an Association?

Consider mapping an existing Association in a manner such as this: ...
8
votes
4answers
299 views

Fast method to select matrix elements based on a vector of positions

There is an operation for which I have long wanted to find a better solution. Let: a be a matrix of dimensions $m\times n$ ...
7
votes
1answer
225 views

Porting code from MATLAB

I'd like to switch from MATLAB to Mathematica for my research in digital signal processing. I used Mathematica a lot during the '90s but I haven't used it since then, so I might as well be a complete ...
2
votes
0answers
62 views

Mathlink program with low cpu usage on linux machines only

Question I am incorporating some c++ code via Mathlink into my simulations in Mathematica. My questions involves the execution efficiency of the c++ code on different machines. For example, on my ...
6
votes
1answer
167 views

Why is AllTrue much slower than VectorQ?

This seems to be a theme with V10, a new function dedicated to a specific task doesn't live up to expectation performance-wise. Mr. Wizard has already uncovered 2 such functions here and here. So how ...
19
votes
3answers
645 views

DeleteDuplicatesBy is not performing as I'd hoped. Am I missing something?

Déjà vu: a new-in-v10 function should provide a better solution to an old problem, but my enthusiasm is curbed when I run timings. This time the function is ...
34
votes
2answers
732 views

Why is the new PositionIndex horribly slow?

This issue has largely been mitigated in 10.0.1. New timings for the final test below are: ...
0
votes
0answers
91 views

Timing: how make a program to work faster using built in functions and tables

I want to improve my code to make it working faster even for very large numbers. ...
5
votes
0answers
208 views

Problems extending NelderMeadMinimize into 2D

Update The eventual route I followed was to adapt a version of the NM-simplex written in C and link to it with MathLink. Once I've tested this out fully I'll post it here. I've been playing around ...
0
votes
1answer
82 views

How can I speed up this multi-equation Solve problem?

I'm trying to solve a set of equations (chemical kinetics for a complex system, but don't worry about that). I ran it for 24 hours and I got no output. Here is the relatively simple code (I want to ...
0
votes
0answers
28 views

Speed up series expansion [duplicate]

I have a rather complicated function that I need to expand it in series, which is quite straightforward but takes a long time. I was wondering if there is a way to speed up such a calculation: ...
-3
votes
1answer
82 views

Why does the function Evaluate speed up one function by orders of magnitude and another function not at all?

I've got two functions, one sample function (function 1) i've created for demonstartion purposes here, and my actual function. The function Evaluate speeds up the sample function by orders of ...
0
votes
1answer
110 views

Why does Evaluate fail? [closed]

If i use this code: ...
1
vote
0answers
42 views

Understanding Mathematica: How to write fast code? [duplicate]

I'm relatively new to Mathematica but have done a few projects using it so I'm quite comfortable with the syntax and the basic functions available. Since I've had programming experience in the past I ...
3
votes
2answers
229 views

How to speed up minimization of function

I have the fallowing code (the functions within that code are replaced by simpler ones, but the code structure is identical): $$$$ ...
1
vote
0answers
82 views

Speeding up the numerical integration of a certain function

I'm using the following function f[t_] := 3/4 t^-3 NIntegrate[(x^4*Csch[x/2]^2), {x, 0, t}, Method -> {Automatic, "SymbolicProcessing" -> 0}]; I ...
0
votes
0answers
69 views

Timing Differences

In More efficient implementation for comparing coefficient lists in a Do-loop link, george2079 posts in his first answer a piece of code that takes 352 seconds to compute. When I run this same piece ...
1
vote
2answers
186 views

More efficient implementation for comparing coefficient lists in a Do-loop

I'm wondering how to speed up the following code so it actually computes for $n=2$: ...
7
votes
4answers
440 views

Generating an Ulam spiral

An Ulam Spiral is quite an interesting construction, revealing unexpected features in the distribution of primes. Here is a related topic with one answer by Pinguin Dirk, who has provided one ...
7
votes
3answers
361 views

Speed up ContourPlot3D

I have two issues with the following code. (1) It executes too slowly. If I use "Speed" instead of "Quality" for PerformanceGoal, I get the speed but an undesirable delay in proper rendering of the ...
2
votes
4answers
172 views

Replacing High Densities of 1.s in Very Large Binary Arrays

I am working with very large binary arrays on the order of {100,100000}. It may look something like this. ...
10
votes
0answers
115 views

Exp is slower for some number ranges

I get a hit on performance with the Exp function depending on the range of my inputs. for example, with: ...
3
votes
0answers
139 views

Module in numerical model for NonlinearModelFit is slow and leaks memory

I am trying to make a NonlinearModelFit where the model is the result of a numerical calculation. My code works, but it is very slow and also seems to have a memory ...
1
vote
1answer
140 views

Speeding up a function

This is un updated version of a previous (now deleted) post of mine. I need to marginalize a distribution. I was suggested to use the code in this thread. This works just fine, but the problem is ...
3
votes
1answer
268 views

How to efficiently drag points on a curve within a Manipulate

My intention is to demonstrate on any conic various metrics properties on points belonging to the conic and connected by lines. I want the points to be dynamically moved not the usual way, ...
5
votes
3answers
229 views

Split list of states by run lengths and discontinuous changes in state

I was asked to look into writing some simulation code for a user, where the simulation output (x) would oscillate between three possible states: ...
0
votes
0answers
80 views

How to save the results in an identical Excel file (.xls) in each iteration of a Do loop

Assume the following code (Below code is just an example to clearly illustrate my problem): ...
6
votes
1answer
107 views

Garbage collection for memoized functions on subkernels

I have a top-level function which operates on some data: findBestIntegers[data_]:=ParallelMap[optimize,data] which makes parallelized calls to an optimization ...