time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

commit 071bf00d7ab69fca045fbc7494c5d973f687fc6f
parent 72884d8e1e281a1b8b65fd53d0b380c91ae9d8e4
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Thu,  1 Dec 2022 16:10:29 +0000

fix: improve warnings for a check which should never fail

Diffstat:
MC/samples/samples | 0
MC/samples/samples.c | 5+++--
MR/samples.R | 3+++
Mjs/samples.js | 3+++
Mpython/samples.py | 3++-
5 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/C/samples/samples b/C/samples/samples Binary files differ. diff --git a/C/samples/samples.c b/C/samples/samples.c @@ -4,7 +4,7 @@ #include <gsl/gsl_rng.h> #include <gsl/gsl_randist.h> -#define N 10000000 +#define N 1000000 /* * For very high values of N, you will want to increase the maximum stack trace, otherwise you will suffer a segmentation fault * In Ubuntu/bash you can do this with $ ulimit -Ss 256000 ## ~256Mbs @@ -89,9 +89,10 @@ void mixture(gsl_rng * r, double *dists[], double *weights, int n, double *resul } } if(index_found == 0) { - printf("\nThis shouldn't have happened"); + printf("\nThis shouldn't be able to happen"); // gsl_rng_free (r); // abort(); // this shouldn't have happened. + }else{ int sample_index = (int) floor(p_2 * N); results[i] = dists[index_counter][sample_index]; diff --git a/R/samples.R b/R/samples.R @@ -25,6 +25,9 @@ mixture <- function(samples_list, weights_array, n=DEFAULT_N){ # note that this for(i in c(1:n)){ helper_which_list = which(cummulative_sums > helper_probs[i]) # helper_loc = ifelse(is.na(helper_which_list[1]), 1, helper_which_list[1]) + if(is.na(helper_which_list[1])){ + print("This should never happen") + } helper_loc = helper_which_list[1] target_samples = samples_list[[helper_loc]] result = sample(target_samples, 1) diff --git a/js/samples.js b/js/samples.js @@ -38,6 +38,9 @@ const mixture = (dists_array, weights_array, n = DEFAULT_N) => { const helper_probs = [...new Array(n)].map(_ => Math.random()) const results = helper_probs.map(p => { let match_index = cummulative_sums.findIndex(x => x > p) + if(match_index == -1){ + console.log("Error: This should never happen.") + } let target_loc = match_index // == -1 ? 0 : match_index let target_samples = dists_array[target_loc] return target_samples[Math.floor(Math.random() * target_samples.length)]; diff --git a/python/samples.py b/python/samples.py @@ -32,7 +32,8 @@ def mixture(samples_list, weights_array, n=DEFAULT_N): helper_list = [j for j in range( len(cummulative_sums)) if cummulative_sums[j] > helper_probs[i]] if len(helper_list) == 0: - helper_loc = 0 + helper_loc = 0 # continue + print("This should never happen") else: helper_loc = helper_list[0] target_samples = samples_list[helper_loc]