freemarker怎么遍历map集合
后台:Map> map = new HashMap>(); html: <#if map?exists> <#list map?keys as key> ${key} 现在我想获取value中的值,html中该怎么写
最满意答案
我对你的代码做了一些小调整而没有改变它的整体风格。 注意Yoong Kim的建议并尝试将代码分解成更小的部分,以使其更具可读性和可维护性,这将是一件好事。
您的函数现在获得两个“n”参数,表示每行中有多少个样本,以及您需要多少次迭代(列)。
你在循环中增加了数组
Median
和Mean
,这需要很多关于重新分配内存和复制内容的麻烦,这会减慢一切。 我已经预定义X_after
并在循环后移动平均值和中值计算以避免这种情况。 (作为奖励,mean
和median
仅被调用一次而不是n_iteration
次。)对
ifelse
的调用并不是真的需要。调用
rlnorm
一次,为x和lod生成足够的值比调用它两次要快一点。这是更新的功能。
dset2 <- function (mu, sigma, n_samples, n_iterations, p) { X_after <- matrix(NA_real_, nrow = n_iterations, ncol = n_samples) pct_cens <- numeric(n_iterations) count <- 1 while(count <= n_iterations) { random_values <- rlnorm(2L * n_samples, log(mu), log(sigma)) lod <- quantile(random_values[1:n_samples], p = p) X_before <- random_values[(n_samples + 1L):(2L * n_samples)] X_after[count, ] <- pmax(X_before, lod) delta <- X_before <= lod pct_cens[count] <- mean(delta) if (pct_cens > 0 && pct_cens < 1 ) count <- count + 1 } Median <- apply(X_after, 1, median) Mean <- rowMeans(X_after) data.frame(Pct_cens_array=pct_cens, Mean=Mean, Median=Median) }
比较时间,例如,
mu=1 sigma=3 n_samples=10L n_iterations = 1000L p=0.10 system.time(dset(mu,sigma, n_samples, n_iterations, p)) system.time(dset2(mu,sigma, n_samples, n_iterations, p))
在我的机器上,有3倍加速。
I've made a few little tweaks to your code without changing the whole style of it. It would be good to heed Yoong Kim's advice and try to break up the code into smaller pieces, to make it more readable and maintainable.
Your function now gets two "n" arguments, for how many samples you have in each row, and how many iterations (columns) you want.
You were growing the arrays
Median
andMean
in the loop, which requires a lot of messing about reallocating memory and copying things, which slows everything down. I've predefinedX_after
and moved the mean and median calculations after the loop to avoid this. (As a bonus,mean
andmedian
only get called once instead ofn_iteration
times.)The calls to
ifelse
weren't really needed.It is a little quicker to call
rlnorm
once, generating enough values for x and the lod, than to call it twice.Here's the updated function.
dset2 <- function (mu, sigma, n_samples, n_iterations, p) { X_after <- matrix(NA_real_, nrow = n_iterations, ncol = n_samples) pct_cens <- numeric(n_iterations) count <- 1 while(count <= n_iterations) { random_values <- rlnorm(2L * n_samples, log(mu), log(sigma)) lod <- quantile(random_values[1:n_samples], p = p) X_before <- random_values[(n_samples + 1L):(2L * n_samples)] X_after[count, ] <- pmax(X_before, lod) delta <- X_before <= lod pct_cens[count] <- mean(delta) if (pct_cens > 0 && pct_cens < 1 ) count <- count + 1 } Median <- apply(X_after, 1, median) Mean <- rowMeans(X_after) data.frame(Pct_cens_array=pct_cens, Mean=Mean, Median=Median) }
Compare timings with, for example,
mu=1 sigma=3 n_samples=10L n_iterations = 1000L p=0.10 system.time(dset(mu,sigma, n_samples, n_iterations, p)) system.time(dset2(mu,sigma, n_samples, n_iterations, p))
On my machine, there is a factor of 3 speedup.
相关问答
更多-
在r中优化嵌套循环(Optimize nested for loop in r)[2022-05-05]
循环遍历行是不必要的,你可以用ifelse矢量ifelse循环: x[1:3] <- lapply(1:3, function(n) ifelse(x[[n+3]] == 0, NA, x[[n]])) x # c1 c2 c3 c4 c5 c6 #r1 1 4 7 4 3 2 #r2 NA 5 8 0 8 8 #r3 3 NA 9 9 0 5 #r4 2 2 NA 1 7 0 #r5 1 3 1 5 3 5 #r6 NA 4 6 0 6 ... -
如何在循环中重新编码以优化R中大型仿真的性能?(How to recode while loop to optimize performance for large simulation in R?)[2023-12-17]
我对你的代码做了一些小调整而没有改变它的整体风格。 注意Yoong Kim的建议并尝试将代码分解成更小的部分,以使其更具可读性和可维护性,这将是一件好事。 您的函数现在获得两个“n”参数,表示每行中有多少个样本,以及您需要多少次迭代(列)。 你在循环中增加了数组Median和Mean ,这需要很多关于重新分配内存和复制内容的麻烦,这会减慢一切。 我已经预定义X_after并在循环后移动平均值和中值计算以避免这种情况。 (作为奖励, mean和median仅被调用一次而不是n_iteration次。) 对if ... -
虽然您使用的是Devectorize.jl包,但我建议您将所有这些矢量化操作明确地写成简单的循环。 我预计这会给你带来显着的性能提升。 Devectorize包当然是一个伟大的贡献,但为了看到跳过它来为你做肮脏的工作,你可以做这样的事情(来自包README的一个例子): using Devectorize a = rand(2,2); b = rand(2,2); c = rand(2,2); julia> macroexpand(:(@devec r = exp(a + b) .* sum(c))) ...
-
基于R优化重新编码(Optimize recode in base R)[2023-06-25]
这是一个通用(可扩展)的方法,速度也非常快: sets <- list(A, B, C, D, E, G) vals <- c(1, 2, 3, 4, 5, 7) keys <- unlist(sets) values <- rep(vals, sapply(sets, length)) df$foo <- values[match(df$foo, keys)] Here is a general (scalable) approach, very fast too: sets <- list ... -
如果一切都失败了,你可以结合使用这两种方法: 再次猜测环境:问问你自己如何实现它的功能,然后推断哪些功能可能需要最少的计算工作。 试错法:只是通过测试它们来比较不同的方法。 如果环境有一定的代码定时功能,例如(准确地)告诉你现在是什么时间的函数,那么这会有很大的帮助。 不要忘记内存缓存和优化等效果。 如果您尝试在特定环境中使用特定功能,它可能会与您以前的体验有所不同。 If all else fails, you can use a combination of these two approaches: ...
-
如果您只想在数字0,1,...,9上获得离散的均匀分布,那么只需使用样本 sample(0:9, k, replace = TRUE) 使用你现在拥有的代码,你实际得到的概率为0.05,每个得到0或10,概率为.10,得到1-9。 If all you want to do is get a discrete uniform distribution on the numbers 0, 1, ..., 9 then just use sample sample(0:9, k, replace = TRUE ...
-
我认为dplyr更整洁。 正确使用recode是个好主意。 mutate_all()可用于对整个数据帧进行操作, mutate_at()仅对所选变量进行操作。 有很多方法可以在dplyr中指定变量。 mydata <- data.frame(arg1=c(1,2,4,5),arg2=c(1,1,2,0),arg3=c(1,1,1,1)) mydata mydata <- mydata %>% mutate_at(c("arg1","arg2"), funs(recode(., `1`=-1, ...
-
优化For循环 - R.(Optimize the For loop - R)[2021-07-13]
在许多方面,可以改进此代码并使其更快。 看起来你基本上是在编写C风格的代码,而不是利用内置的矢量化R函数。 这是一个例子。 这部分代码: DD <- vector(length = R) ConceptIDSource <- MITMatrix[Source,q] counterq <- 1 # counterq is a pointer to cell of vector DD that keep the co ... -
在使用嵌套循环时,检查outer()是否不适合你的工作总是很有趣。 outer()是嵌套循环的向量化解决方案; 它将向量化函数应用于前两个参数中元素的每个可能组合。 由于stringdist()对向量起作用,因此您可以简单地执行: library(stringdist) strings <- c("Hello", "Helo", "Hole", "Apple", "Ape", "New", "Old", "System", "Systemic") outer(strings,s ...
-
优化R中的循环(Optimize loop in R)[2022-06-21]
SIN_FM5 <- data.frame(Combination = sample(1:10, 100, repl=TRUE), MONTANT_PAIEMENT=rnorm(100)) bySIN <- by(SIN_FM5, list(SIN_FM5[['Combination']]), FUN= function(subd) { data.frame(counts = nrow(subd), meanMont = mean(subd$MONT ...