CreditMetrics is a package of functions for computing the CreditMetrics risk model. Because this can be a computationally-intensive exercise, we thought it would be a good example for comparing the performance of Community R (version 2.7.2 from CRAN) and REvolution R (based on R 2.7.2, but compiled with optimized math libraries).
In the process of setting up the benchmarking exercise, a REvolution engineer noticed a performance improvement that can be applied to the CreditMetrics script itself. The cm.val function in the original script (version 0.0.1) collected results from the iterative process by repeatedly pasting (with rbind) a row of data onto an existing matrix, like this:
simV <- rbind(simV, V[i, simClasses])
The problem with including an expression like this in a loop is that the entire simV object must be remade with each iteration of the loop, and all of the data is copied each time -- even the previous rows which are not modified. A much more efficient way of doing this is to declare a large matrix of zeroes at the outset:
simV <- matrix(0,N,n)
and then replace a row of the matrix with each iteration:
simV[i,] <- V[i,simClasses]
This means only one row of data is copied with each iteration, instead of the entire matrix, and dramatically improves the performance of the function. This change has been accepted by the CreditMetrics package author, Andreas Wittmann, and is now included in version 0.0.2 of CreditMetrics on CRAN.
With that tweak to the code in place, we were ready to compare the performance of CreditMetrics on Community R and REvolution R using a benchmark script. We compared REvolution R with the Windows binary distribution of R 2.7.2 downloaded from CRAN. CreditMetrics 0.0.2 was used in both cases. You can find the benchmark and details about the benchmarking process on the main REvolution Computing website, but the result is that the REvolution R runs the benchmark more than 10 times faster than the equivalent version of Community R.
Of course, this particular example was chosen because it makes heavy use of the types of computations that are accelerated by the optimized math library in REvolution R, such as matrix multiplication and Cholesky decomposition. Also, the benchmark was performed on an 8-core workstation: REvolution R is able to parallelize many computations across multiple cores, which can dramatically improve performance for these kinds of calculations. But it does provide a great example of how on a powerful workstation, using REvolution R can provide significant performance improvements without making any changes to your code.
REvolution Computing: CreditMetrics benchmarks.
Comments