Questions tagged [performance-tuning]

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.

Filter by
Sorted by
Tagged with
1 vote
0 answers
40 views

How to produce the following table faster?

Consider two data files, acc.m and distr.dat. They contain some functions that I need to multiply to obtain function ...
  • 5,102
3 votes
2 answers
111 views

How can I speed up calculation of the integral?

Is it possible to speed up the calculation of the Kx integral? ...
  • 759
3 votes
0 answers
67 views

Compiling a function that uses MapThread

I'm looking for tips on quickly applying the following update to $B$ pairs of $(w_i,x_i)$ $$\text{step}(w_i,x_i)=w_i-a x_i \langle w_i, x_i \rangle$$ Below is a readable but very slow version ...
5 votes
1 answer
93 views

Minimizing computational time for a quantum walk problem

Any suggestions as to how to speed up the computational time for this quantum walk problem which is coded using a normalized SparseArray coin operator as follows: ...
  • 3,128
7 votes
5 answers
236 views

Values of counting functions

I have a table of integers, say A, and consider the counting function C, which, for a given x, gives the number of elements in A which are less or equal to x. Next, I want to plot C(x) in some range, ...
1 vote
1 answer
62 views

How to speedup integration of interpolated function with logarithmized data?

Consider some dataset corresponding to the grid x1, x2, function[x1,x2]: ...
  • 5,102
8 votes
2 answers
1k views

Why does Mathematica take so long to produce this sound?

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? ...
0 votes
0 answers
36 views

Efficient evaluation of vector inner products [closed]

I have a situation where I'm trying to quickly evaluate elements of some matrix products. Let $A, B$ be two Hermitian random matrices, and suppose that we have solved the eigenvalue problem for $A$. I ...
  • 647
0 votes
0 answers
19 views

Smoothening noisy distributions to learn eigenvalue statistics

I have a problem involving random matrices, in which I'm trying to learn properties of their spectral statistics. The problem I'm having can be boiled down to a list management issue, but I will begin ...
  • 647
16 votes
4 answers
516 views

Speeding up Gaussian sampling

Edit Greg Hurst answers show how to independently get 3x speed-up using single-threaded MKL implementation and 4x speed-up by using native implementation with multi-threading, can they be combined? ...
1 vote
1 answer
54 views

How to speedup calling the interpolation function? [duplicate]

Consider the following data: ...
  • 5,102
0 votes
0 answers
34 views

Compile a code evaluating a condition only if another condition is positive

Consider the following three tables: ...
  • 5,102
2 votes
1 answer
50 views

How to assign a new value to the table inside Compile?

Consider some tables and function func: phivals=RandomReal[{-Pi,Pi},10^5]; func[phi_]=phi^5*Exp[-phi]; acc=Table[0,10^5]; I ...
  • 5,102
1 vote
1 answer
96 views

Arrange 1-n^2 in n×n grid satisfy products of rows equal to products columns

For which $n\in\mathbb{N}$ is it possible to arrange $\{1,…,n^2\}$ in an $n\times n$ grid so that the set of products of columns equals the set of products of rows? I can find solution for $3\times 3$ ...
  • 5,404
3 votes
1 answer
140 views

Why the ugly code is faster than the pretty code?

The task Consider two tables, Tab1,Tab2, that include some real-number data. I need to evaluate the product of some boolean conditions on the value of some function ...
  • 5,102
0 votes
0 answers
65 views

How to generate points belonging to some region very fast?

Consider two variables Ex,th. th ranges within thmin=10^-5; thmax=Pi/2; while ...
  • 5,102
1 vote
2 answers
105 views

Solving $\sum_i a_i p_i^k=b_0$ for $k$

Is there a fast way to find $k$ in the equation below for positive reals $a_i,p_i,b_0$? $$\sum_i^d a_i p_i^k=b_0$$ Constraint of the problem means $p_i$ is very close to 1, so $p_i \approx \exp(p_i-1)$...
6 votes
1 answer
127 views

How to keep lines on boundary or inside a region?

This post is related to one of my previous posts. My goal is to keep only the lines that fall on the boundary (2, 8) or inside the region ...
  • 2,822
4 votes
3 answers
161 views

Compile code involving two matrix multiplications

Consider the following simple matrix operation ($a_j, b_j, z, w$ are matrices) $$ w=\sum_j a_j\cdot z\cdot b_j$$ For performance reasons I am compiling the code ...
  • 17k
3 votes
1 answer
70 views

How to keep the code compact in the following case?

Consider the following code: ...
  • 5,102
1 vote
2 answers
69 views

How to find the minimal and maximal polar angles covered by the following domain?

Consider some region in 3D space. Example is reg = Cylinder[{{0, 24, 14}, {0, 42.7, 14}}, 9] I want to find the minimal and maximal polar angle $\theta = \arccos\...
  • 5,102
0 votes
3 answers
54 views

Efficient and compact replacement for Association in the following case

Consider the following association: ...
  • 5,102
12 votes
3 answers
272 views

Efficient eigendecomposition of DPR1 matrices

I'm finding that the following bit is the bottleneck in my code ...
3 votes
2 answers
123 views

Position command runs much slower for range containing numerically evaluated Bessel function

I have a long list (about 10000) of positive numbers (here simply denoted list), for which I want to find the positions of elements lying between 0.5 and the first ...
  • 973
0 votes
2 answers
66 views

How to minimize computational time in an if condition?

I have a code as below: ...
1 vote
1 answer
91 views

Why do some functions, or at least, PowersRepresentations, run far faster on subsequent calls? What determines this behaviour?

Say I run the following: PowersRepresentations[4782969,4,2] and it takes about 2 minutes. If I call it again it takes only about 0.0005 seconds. What determines ...
  • 717
13 votes
3 answers
436 views

Why does Module slow down StringMatchQ by 180 times?

I knew that Module is slower than With or Block, often by several times. But it surprises me ...
  • 1,408
0 votes
0 answers
37 views

Why Names will search all subcontexts?

My version is 13.2. When debugging I came across this peculiar feature of Names. Starting with a fresh kernel, define Then ...
  • 1,408
8 votes
1 answer
232 views

Troubleshooting Compile making things slower

I'm trying to speed up the following piece of code which runs in about 0.5 seconds on my machine. ...
0 votes
0 answers
37 views

How to plot a function efficiently without having to recalculate

I have to make a lot of graphs for two different functions Nzzp (which can be computed quickly without problem) and NzzG which requires three numerical integrations in a row so I'm using paralleltable ...
  • 1
0 votes
0 answers
34 views

Reduce max memory requriement for Kuramoto model code

I have some code for running the Kuramoto model on a random graph. Even with 128GB of virtual memory assigned, the routine sometimes exceeds its maximum allowance and is killed (I am running this on a ...
  • 1,619
1 vote
0 answers
57 views

Find all ways to split a list into k sublists (of different length)

I would like to write an efficient code for splitting a set into k disjoint subsets, whose union would be the input set. The input set is represented by sorted lists with no repetitions, and the ...
1 vote
1 answer
41 views

Replacement for Association in the following case

Consider the following three integrals: ...
  • 5,102
4 votes
1 answer
56 views

Quickly summing matrix elements

I have a pair of rank-4 tensors, (T,V), where each index takes four values. I want to quickly contract these with the rank-4 antisymmetric tensor using the following operation: ...
  • 647
2 votes
1 answer
114 views

Efficient memory usage while building a large sparse matrix

I am sure the following problem has been solved already, but I am unable to find any solution... Any help appreciated! So I am building a pretty huge matrix (or tensor, actually) using ...
  • 83
1 vote
0 answers
71 views

Mathematica does not recognize all CPU kernels/cores [closed]

I am using Ubuntu 22 and Mathematica 13. According to the description of my laptop I should have four CPU cores, as seen in the following screenshot: However, Mathematica only seems to recognize two ...
  • 83
4 votes
1 answer
136 views

Why does `MemoryInUse` keep increasing?

Consider MemoryInUse. After kernel initiation, I have noticed that its value keeps increasing, even when not evaluating any cell, as seen ...
  • 4,362
2 votes
1 answer
169 views

Bugs with computational geometry: versions 12.2 vs 13.2

Updated System Info: HONOR laptop AMD Ryzen 5 4600H 3Hz 6 cores 16GB Memory, Windows 10 Pro 64 bit with updates. Mathematica Info: Versions 12.2 and 13.2, both on the same laptop. Examples: ...
  • 793
2 votes
2 answers
105 views

How to gather functions which intersect at the same point?

How to gather functions which intersect at the same point (real solution)? func = {2 x, x^2 + 1, (x^3 + 3)/2}; For example, with the list of functions above I ...
  • 2,263
14 votes
1 answer
432 views

Memory leak with Mathematica Graph functions

I am running Mathematica 13.2 on a Mac OS Monterey. I have a large code that manipulates graphs both in graph form and as adjacency matrices, computes some graph properties, and returns a list. It has ...
  • 143
6 votes
0 answers
85 views

Slow performance of SequenceCases

While working on a solution to another question, I noticed some performance regression slow performance of SequenceCases and friends. Consider a problem of finding zeroes of a function, given a sorted ...
  • 5,021
0 votes
1 answer
125 views

NIntegrate: slow convergence error and speed efficiency [closed]

I am trying to produce a plot but NIntegrate is very slow and making a number of warnings, I have used UnitStep[] to keep the ...
6 votes
2 answers
689 views

NDSolve very slow on 2D heat equation

I am trying to solve the 2D heat equation $$ \begin{cases} u_{t}-u_{x x}-u_{y y}=f \\ u(0, x, y)=\sin (2 \pi x) \sin (2 \pi y) \\ u(t, 0, y)=0 \\ u(t, x, 1)=0 \\ u_{x}(t, 1, y)=2 \pi e^{-t} \sin (2 \...
0 votes
1 answer
107 views

RegionIntersection runs for a very long time and then aborts without returning a result

Is there a good strategy to speed up RegionIntersection computations like the one below: ...
  • 4,917
6 votes
0 answers
81 views

3D region best way to create signed coastal distance?

As a test, I'm trying to create a signed coastal distance map of the world (assumed perfectly spherical), using the first 1800x900 image above, which I converted to the 2nd image above using ...
1 vote
1 answer
39 views

How to refer to calling notebook instead of my personal include? / detector for high-memory-usage cells

Along the lines of a couple previous posts, I coded this function to show you where in your notebook your biggest space-wasters are. Useful for when your file is becoming huge and unmanageable for ...
  • 753
3 votes
2 answers
91 views

improving efficiency of a code

I have the code: ...
20 votes
4 answers
906 views

Optimize search for rational numbers on unit circle?

Crossposted: https://community.wolfram.com/groups/-/m/t/2763509 I'd like to classify rational numbers on a unit circle by the following property: number of digits in the denominator. It relates to an ...
7 votes
3 answers
343 views

What is a good way to compute successive primorials with Mathematica?

Recall that the primorial of a positive integer $n$ is the product of the prime numbers smaller than $n$. One can define a primorial function in Mathematica quite easily: ...
  • 937
5 votes
2 answers
255 views

Speed up replacement of very large alternatives expression

I'm doing a computation which effectively has the same issue as this code: Range@1000000 /. (Alternatives@@RandomSample[Range@1000000,500000]) -> 0 i.e. ...
  • 3,206

1
2 3 4 5
57