Efficiently reading from a dictionary

6 views
Skip to first unread message

Darren Li

unread,
Aug 28, 2025, 10:49:32 PM (9 days ago) Aug 28
to slim-discuss
Hello everyone,

I have a dataframe/dictionary of values from which I want to get a specific value (row X, column Y) many times. To vectorise the process, I use sapply:

phenoFitness_mhw = sapply(ordered_indices_pop, "get_phenoFit_mhw(applyValue, bsi_given_dhw50_df, indices_of_interest, indices_of_quantiles);");

Where the function get_phenoFit_mhw is defined as:

function (float)get_phenoFit_mhw (integer index, lifso bsi_given_dhw50_var, integer indices_of_interest, integer indices_of_quantiles)

{

return(bsi_given_dhw50_var.getValue(asString(indices_of_quantiles[index]))[indices_of_interest[index]]);

}


bsi_given_dhw50_df is the dictionary of values, from which I want to obtain the specific fitness of an individual, given its phenotype (row X) and given the severity of a marine heatwave experienced (column Y). ordered_indices_pop is simply a vector of indices that I'm using to obtain the corresponding phenotype and marine heatwave experienced for each individual, with both being unique for each (because in the model, the heat stress experienced by the individual is assumed to be dependent on its phenotype). 


I know that the above part of the code is the bottleneck based on the profile, and also I saw someone experiencing the same issue here:



where the process is slowed down significantly because sapply() call is doing a dictionary lookup for each value.

I'm wondering how can I make the above code more efficient? I can't do a simple lookup like in the other thread:

destination_densities_3 = densities[destinations];

Because I'm not just reading from a single vector here, but from a dictionary.

Thanks,
Darren
Reply all
Reply to author
Forward
0 new messages