Skip to content

Commit

Permalink
Optimized C code by moving a variable outside of a loop.
Browse files Browse the repository at this point in the history
It seems that the repeated access to `sources(isrc,ig)` causes a slow-down that can be easily avoided by caching the source as:

```python3
double source_val = sources (isrc, ig)
```

From local test on an M1 mac, this yield a speedup of about ~1.18.

Changed variable name for consistency
  • Loading branch information
thurinj committed Nov 27, 2024
1 parent 959d5d6 commit dadde72
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mtuq/misfit/waveform/c_ext_L2.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@ static PyObject *misfit(PyObject *self, PyObject *args) {

// Sum cross-correlations of all components being considered
for (ig=0; ig<NG; ig++) {
double source_val = sources (isrc, ig);
for (it=0; it<NPAD; it++) {
cc(it) += greens_data(ista,ic,ig,it) * sources(isrc,ig);
cc(it) += greens_data(ista,ic,ig,it) * source_val;
}
}
}
Expand Down

0 comments on commit dadde72

Please sign in to comment.