Thank you, Radford. I should definitely use pqR!

]]>The R code is also much faster in pqR-2020-07-23 (see pqR-project.org) than in R-4.0.2 (1.2 seconds vs. 8.0 seconds on the computer I’m using at the moment). Further investigation reveals that most of the time is spent in ‘sort’, which has a faster implementation in pqR.

]]>Thanks for the alternative! (I was not attempting at being fast, esp. when using R.)

]]>“`

import numpy as np

import time

start = time.time()

mat = np.random.uniform(size=3 * int(1e5)).reshape((int(1e5), 3))

simz = np.apply_along_axis(np.sort, 1, mat)

a = np.mean(

(simz[:, 0] > 0.5) * simz[:, 0]

+ (simz[:, 0] 0.5) * (simz[:, 1] – simz[:, 0])

+ (simz[:, 1] 0.5) * (simz[:, 2] – simz[:, 1])

+ (simz[:, 2] < 0.5) * (1 – simz[:, 2]))

end = time.time()

print(end – start)

“`

Clocks in at around 0.3 secs on my laptop vs 5 secs for your r code.

]]>