Optimizing the code in starkr

Initial library calls

library(starkr)
library(dplyr)
library(ggplot2)
library(profvis)
library(rbenchmark)

First I need to get a benchmark for how my functions run. Particularly, I want to look at the speed at which stark_matrix() runs. I assume the bottleneck is in radial_matrix_element().

states <- starkr::state_list(15, 15, 1/2, 0, 0)
n <- states[, 1]
l <- states[, 2]
j <- states[, 3]
m_j <- 1/2
profvis(starkr::stark_matrix(n, l, j, m_j))

The output from here looks like the vast majority of the bottleneck is in fact in radial_matrix_element() with a small portion of the bottleneck going to clebsch_gordan() and sphere_mat_element(). Now to dive in a little deeper with a look specifically at radial_matrix_element(). I've created a separate file for the original function that I am optimizing with old appendend to the front.

profvis(starkr::old_radial_matrix_element(15, 15, 0, 1, 1/2, 1/2))

The bottleneck appears to be specifically in a line of code where a data frame is produced. I think that this line is actually no longer used in the code and is a left over from a previous iteration. A new version of the function called optim_radial_matrix_element() has been created with that line commented out.

profvis(starkr::optim_radial_matrix_element(15, 15, 0, 1, 1/2, 1/2))

Newly optimized function seems to give about a factor of 5 speedup.

benchmark(old_radial_matrix_element(15, 15, 0, 1, 1/2, 1/2), optim_radial_matrix_element(15, 15, 0, 1, 1/2, 1/2))

Quick double check to verify that the two functions still give the same answer.

a <- old_radial_matrix_element(15, 15, 0, 1, 1/2, 1/2)
b <- optim_radial_matrix_element(15, 15, 0, 1, 1/2, 1/2)

a == b


bgrich/starkr documentation built on May 12, 2019, 8:21 p.m.