c# - Java vs .NET performance -
i got myself surprised after wrote small code compare .net 4.5 , java 8 performance in computer:
class arraytest { public int[][] jagged; public arraytest(int width, int height) { height = height; width = width; random rng = new random(); jagged = new int[width][]; (int = 0; < height; i++) { jagged[i] = new int[width]; (int j = 0; j < jagged[i][j]; j++) { jagged[i][j] = rng.next(2048); } } } public int this[int i, int j] { { return jagged[i][j]; } set { jagged[i][j] = value; } } public void domath(arraytest a) { (int = 0; < height; i++) { (int j = 0; j < width; j++) { this[i, j] *= a[i, j]; } } } public int height { get; private set; } public int width { get; private set; } } class program { static void main(string[] args) { random rng = new random(); const int loop = 10; int width = 10000, height = 10000; arraytest a1 = new arraytest(width, height), a2 = new arraytest(width, height); stopwatch sw = new stopwatch(); sw.start(); (int = 0; < loop; i++) { a1.domath(a2); } sw.stop(); console.writeline("time taken: " + sw.elapsedmilliseconds); console.readkey(); } } here java version:
public class arraytest { private int width, height; private int[][] array; public arraytest(int width, int height) { this.width = width; this.height = height; array = new int[height][width]; random rng = new random(); (int = 0; < height; i++) { (int j = 0; j < width; j++) { array[i][j] = rng.nextint(2048); } } } public int getwidth() { return width; } public void setwidth(int width) { this.width = width; } public int getheight() { return height; } public void setheight(int height) { this.height = height; } public int[][] getarray() { return array; } public void domath(arraytest a) { (int = 0; < height; i++) { (int j = 0; j < width; j++) { array[i][j] *= a.getarray()[i][j]; } } } } public class main { public static void main(string[] args) { // todo auto-generated method stub final int loops = 10; int width = 10000, height = 10000; arraytest a1 = new arraytest(width, height), a2 = new arraytest(width, height); long start, end; start = java.lang.system.currenttimemillis(); (int = 0; < loops; i++) { a1.domath(a2); } end = java.lang.system.currenttimemillis(); system.out.println("elapsed time: " + (float)(end - start)); } } in computer c# code taking 5200ms run, , java version taking 2800ms(!!!). expecting .net version run faster (or @ least close to) java, got surprised result.
note: ran .net version compiled in release mode, outside visual studio.
can explain result? right? how rewrite code c#.net version gets closer java 1 in execution speed?
[edit]
well, know not valid benchmarking or fair comparison, how rewrite code c#.net version gets closer java 1 in execution speed? tried in forms (manipulating jagged array directly, via getter, etc), test ran slower.
[edit 2]
edited test have width , height = 500, , loop = 5000. 6300ms .net version, , 3700ms java version. ran test multiple times each version, of course.
[edit 3]
just wrote similar test using flat arrays instead of 2d arrays, , time .net version runs equally or faster java. it? c#.net's jagged arrays slower java's multidimensional arrays?
any time kind of benchmarking or performance analysis, need ask lot of questions, , take particular result (to paraphrase tanenbaum), "a metric ton of salt".
however ....
i copied pasted code, , got:
compiler version timing -------- ------- ------ msvs 2012 c# 5 4448.0 eclipse luna jre 1.7 977.0 so java program ran 4.5x faster c# program.
i ran msvs "profiling wizard":
https://msdn.microsoft.com/en-us/library/dd264959.aspx
as might able see screenshot, "big pig" in profile arraytest.get_item(int32, int32), consumed nearly half of execution time:
Comments
Post a Comment