Skip to content

Instantly share code, notes, and snippets.

> library(parallel) # load the library
# create a cluster, which is made up by "detectCores()" of workers
> cl <- makePSOCKcluster(rep("localhost",detectCores()))
> use_multi_core_computing <- function(mat, threshold){
result <- parSapply(cl,mat, function(x){ # parSapply() can be treated as a multi-core version sapply()
if(x > threshold){
x ** 2
}else{
sqrt(x)
}
> set.seed(123)
> N <- 10
> fake.vector <- runif(N, min = 0, max = 10)
# and this time, let's tested 1000 times
> microbenchmark(use_for_loop(fake.vector,5),use_apply_family_funcs(fake.vector,5),times = running_times)
Unit: microseconds
expr min lq mean median uq max neval cld
use_for_loop(fake.vector, 5) 15.396 18.160 22.09172 19.343 27.239 62.372 1000 a
use_apply_family_funcs(fake.vector, 5) 21.712 24.081 28.98297 25.660 34.542 138.165 1000 b
> set.seed(123)
> N <- 10000
> fake.vector <- runif(N, min = 0, max = 10)
# Here we want to use fake.vector to generate a new vector, without modifying the fake.vector
> use_for_loop <- function(mat, threshold) {
result <- numeric(length(mat))
for(i in 1:length(mat)) {
if(mat[i] > threshold){
result[i] <- mat[i] ** 2
> ret <- numeric(100000)
> for_fun <- function(x){
for(i in 1:x){
ret[i] <- i^2
}
}
# firstly, type "?microbenchmark" to check its user doc
> microbenchmark(for_fun(100000), times = 100)
Unit: milliseconds
> ret <- numeric(100000)
> for_fun <- function(x){
for(i in 1:x){
ret[i] <- i^2
}
}
> system.time(for_fun(100000))
user system elapsed
0.13 0.00 0.13
@JackHo327
JackHo327 / iterm2.md
Created April 11, 2018 05:21 — forked from squarism/iterm2.md
iterm2 cheatsheet

Tabs and Windows

Function Shortcut
Fullscreen + Enter
Previous Tab + Left Arrow
Next Tab + Right Arrow
Go to Tab + Number
Go to Window + Option + Number
Go to Split Pane by Direction + Option + Arrow
@JackHo327
JackHo327 / git-feature-workflow.md
Created March 15, 2018 20:30 — forked from blackfalcon/git-feature-workflow.md
Git basics - a general workflow

There are many Git workflows out there, I heavily suggest also reading the atlassian.com [Git Workflow][article] article as there is more detail then presented here.

The two prevailing workflows are [Gitflow][gitflow] and [feature branches][feature]. IMHO, being more of a subscriber to continuous integration, I feel that the feature branch workflow is better suited.

When using Bash in the command line, it leaves a bit to be desired when it comes to awareness of state. I would suggest following these instructions on [setting up GIT Bash autocompletion][git-auto].

Basic branching

When working with a centralized workflow the concepts are simple, master represented the official history and is always deployable. With each now scope of work, aka feature, the developer is to create a new branch. For clarity, make sure to use descriptive names like transaction-fail-message or github-oauth for your branches.

test