Created
November 14, 2023 00:49
-
-
Save MichaelChirico/7706fe44cf29b5e12007bf4e1c1f73ce to your computer and use it in GitHub Desktop.
grep() slower than which(grepl())??
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(microbenchmark) | |
N = 1e4 | |
v = sample(letters, N, TRUE) | |
microbenchmark(times = 200L, grep("a", v), which(grepl("a", v)), grep("[a-m]", v), which(grepl("[a-m]", v)), grep("[a-z]", v), which(grepl("[a-z]", v))) | |
# Unit: microseconds | |
# expr min lq mean median uq max neval cld | |
# grep("a", v) 630.561 640.4110 670.5257 655.4915 680.6115 866.132 200 b | |
# which(grepl("a", v)) 598.802 609.5220 637.0542 621.0965 652.9120 839.231 200 a | |
# grep("[a-m]", v) 692.601 708.4215 735.6788 721.3065 744.6365 1207.722 200 d | |
# which(grepl("[a-m]", v)) 652.362 671.8470 704.9352 687.3010 719.2070 1007.502 200 c | |
# grep("[a-z]", v) 639.452 649.2360 683.5104 661.7870 688.1360 1161.722 200 b | |
# which(grepl("[a-z]", v)) 589.322 598.3860 629.4264 607.7310 639.5370 1113.842 200 a | |
Ditto for N=1e7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment