Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中有效地重新排列字符串中的字母?_R_Performance_Function - Fatal编程技术网

如何在R中有效地重新排列字符串中的字母?

如何在R中有效地重新排列字符串中的字母?,r,performance,function,R,Performance,Function,我有以下函数来重新排列字符向量中的字母 重新排序字母[1]“ERRTTY”“ABC”“DEF” 由(v0.3.0)于2020年4月29日创建 基本上我想返回相同的字符,但带有大写字母和排序顺序 目前,运行150万长度的矢量大约需要1分钟 编辑:我还尝试使用future进行并行化。应用package,它比base R解决方案快3倍(也很容易修改当前代码) reorder\u letter可能utf8ToInt和intToUtf8比strsplit和paste更快 x <- c("trErt

我有以下函数来重新排列字符向量中的字母

重新排序字母[1]“ERRTTY”“ABC”“DEF”
由(v0.3.0)于2020年4月29日创建

基本上我想返回相同的字符,但带有大写字母和排序顺序

目前,运行150万长度的矢量大约需要1分钟

编辑:我还尝试使用
future进行并行化。应用
package,它比base R解决方案快3倍(也很容易修改当前代码)


reorder\u letter可能
utf8ToInt
intToUtf8
strsplit
paste
更快

x <- c("trErty","Bca","def")
unlist(lapply(x, function(y) {intToUtf8(sort(utf8ToInt(toupper(y))))}))
#[1] "ERRTTY" "ABC"    "DEF"   
要了解什么会占用大量计算时间,您可以使用
Rprof
,例如:

reorder_letter <- function(x) { #Function
  sapply(strsplit(x,split = ""),function(x) paste(sort(toupper(x)),collapse = ""))}
x <- apply(expand.grid(letters, LETTERS, letters), 1, paste, collapse = "") #Data

Rprof()
y <- reorder_letter(x)
Rprof(NULL)
summaryRprof()
#$by.self
#               self.time self.pct total.time total.pct
#"FUN"               0.12    20.69       0.54     93.10
#"sort.int"          0.10    17.24       0.22     37.93
#"paste"             0.08    13.79       0.42     72.41
#"sort"              0.06    10.34       0.34     58.62
#"sort.default"      0.06    10.34       0.28     48.28
#"match.arg"         0.04     6.90       0.10     17.24
#"eval"              0.04     6.90       0.04      6.90
#"sapply"            0.02     3.45       0.58    100.00
#"lapply"            0.02     3.45       0.56     96.55
#".doSortWrap"       0.02     3.45       0.02      3.45
#"formals"           0.02     3.45       0.02      3.45
#
#$by.total
#                 total.time total.pct self.time self.pct
#"sapply"               0.58    100.00      0.02     3.45
#"reorder_letter"       0.58    100.00      0.00     0.00
#"lapply"               0.56     96.55      0.02     3.45
#"FUN"                  0.54     93.10      0.12    20.69
#"paste"                0.42     72.41      0.08    13.79
#"sort"                 0.34     58.62      0.06    10.34
#"sort.default"         0.28     48.28      0.06    10.34
#"sort.int"             0.22     37.93      0.10    17.24
#"match.arg"            0.10     17.24      0.04     6.90
#"eval"                 0.04      6.90      0.04     6.90
#".doSortWrap"          0.02      3.45      0.02     3.45
#"formals"              0.02      3.45      0.02     3.45
#
#$sample.interval
#[1] 0.02
#
#$sampling.time
#[1] 0.58

reorder\u字母我认为这是一个骗局,尽管我不确定是否有比你现有的更好的。这是否回答了你的问题?哇
C++
是性能之王!我还想知道,你有没有找到一个写函数的瓶颈的想法?例如,r中的这个函数看起来像是
toupper
很好。瓶颈可能是
拆分
排序
粘贴
。有什么好办法来调查这个问题吗?就像调查之后一样,拆分需要30%的时间,排序需要40%的时间……然后我可能会尝试在
C++
中编写瓶颈,或者用其他高性能软件包替换。为此,您可以使用分析器。我添加了一个带有
Rprof
的简单示例。
reorder_letter <- function(x) { #Function
  sapply(strsplit(x,split = ""),function(x) paste(sort(toupper(x)),collapse = ""))}
x <- apply(expand.grid(letters, LETTERS, letters), 1, paste, collapse = "") #Data

Rprof()
y <- reorder_letter(x)
Rprof(NULL)
summaryRprof()
#$by.self
#               self.time self.pct total.time total.pct
#"FUN"               0.12    20.69       0.54     93.10
#"sort.int"          0.10    17.24       0.22     37.93
#"paste"             0.08    13.79       0.42     72.41
#"sort"              0.06    10.34       0.34     58.62
#"sort.default"      0.06    10.34       0.28     48.28
#"match.arg"         0.04     6.90       0.10     17.24
#"eval"              0.04     6.90       0.04      6.90
#"sapply"            0.02     3.45       0.58    100.00
#"lapply"            0.02     3.45       0.56     96.55
#".doSortWrap"       0.02     3.45       0.02      3.45
#"formals"           0.02     3.45       0.02      3.45
#
#$by.total
#                 total.time total.pct self.time self.pct
#"sapply"               0.58    100.00      0.02     3.45
#"reorder_letter"       0.58    100.00      0.00     0.00
#"lapply"               0.56     96.55      0.02     3.45
#"FUN"                  0.54     93.10      0.12    20.69
#"paste"                0.42     72.41      0.08    13.79
#"sort"                 0.34     58.62      0.06    10.34
#"sort.default"         0.28     48.28      0.06    10.34
#"sort.int"             0.22     37.93      0.10    17.24
#"match.arg"            0.10     17.24      0.04     6.90
#"eval"                 0.04      6.90      0.04     6.90
#".doSortWrap"          0.02      3.45      0.02     3.45
#"formals"              0.02      3.45      0.02     3.45
#
#$sample.interval
#[1] 0.02
#
#$sampling.time
#[1] 0.58