473,883 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

benchmarks? java vs .net

The shootout site has benchmarks comparing different languages. It
includes C# Mono vs Java but not C# .NET vs Java. So I went through
all the benchmark on the site ...

http://kingrazi.blogspot.com/2008/05...enchmarks.html

Just to keep the post on topic for my friends at comp.lang.c++, how do
I play default windows sounds with C++?

Jun 27 '08
358 13276
Jon Harrop wrote:
Jon Skeet [C# MVP] wrote:
Jon Harrop <jo*@ffconsulta ncy.comwrote:
That is Java outperforming C#, not .NET.
No, it's Java outperforming .NET calling a virtual method. It has
nothing to do with the code that the IL is generated in: if you have a
virtual method which isn't overridden, the .NET CLR will not inline
calls to it. The Hotspot VM will inline it until it first sees the
method being overridden. The difference here is in the jitting model,
not in the language.

Try it in F#, ensuring that the method is virtual and actually called
each time (consider that the method might be one which has side-
effects). I can't see how it would possibly have a different result.

This optimization can obviously be done statically by the compiler.
No - this optimization cannot be done statically for runtimes that
support dynamic code loading, as the CLR and JVM do. If the compiler
tries to replace 'callvirt' with 'call', the assembly will no longer be
verifiable, and if the assembly is dynamically loaded into the context
of another application, the virtual call semantics will be violated.

For example:

---8<---
using System;

public class B
{
public virtual void M()
{
Console.WriteLi ne("Hello from B");
}
}

public class App
{
static void Main()
{
CallM(new B());
}

public static void CallM(B instance)
{
instance.M();
}
}
--->8---

Compile this App.cs to App.exe, then disassemble with:

ildasm App.exe /out:App.il

Then edit App.il so that CallM is written thusly, applying the
"optimizati on":

---8<---
.method public hidebysig static void CallM(class B 'instance') cil
managed
{
// Code size 9 (0x9)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0
IL_0002: call instance void B::M()
// (above was 'callvirt')
IL_0007: nop
IL_0008: ret
} // end of method App::CallM
--->8---

Assemble with:

ilasm App.il

Verify with:

peverify App.exe

Observe error:

---8<---
Microsoft (R) .NET Framework PE Verifier. Version 3.5.21022.8
Copyright (c) Microsoft Corporation. All rights reserved.

[IL]: Error: [c:\proj\cli-callvirt\App.ex e : App::CallM][offset
0x00000002] The 'this' parameter to the call must be the calling
method's 'this' parameter.
1 Error Verifying App.exe
--->8---

For a concrete example of virtual method call semantic violation,
consider a second application, App2.cs:

---8<---
using System;

public class D : B
{
public override void M()
{
Console.WriteLi ne("Hello from D!");
}
}

class App2
{
static void Main()
{
App.CallM(new D());
}
}
--->8---

Compile this with:

csc App2.cs /r:App.exe

Run App2.exe when App.exe is the version assembled from App.il modified
to use call instead of callvirt, and observe:

---8<---
Hello from B
--->8---

Recompile the original App.cs from source, then run App2, and observe:

---8<---
Hello from D!
--->8---

- the "obvious optimization" has broken virtual method call semantics.

Implementing the "optimizati on" for strictly private classes is not
useful either, as reflection with appropriate permissions, along with
runtime code generation, can see through such visibility constraints.

-- Barry

--
http://barrkel.blogspot.com/
Jun 27 '08 #201
On Thu, 05 Jun 2008 10:59:58 +0100, Jon Harrop <jo*@ffconsulta ncy.com>
wrote:
>Again, the fast .NET code (and compilation instructions) are already on the
shootout site. There is nothing for me to do (except tell you the same
thing over and over).
F# was much slower than C# and slowest of all...

$ time binarytreesf 20 (F#)
stretch tree of depth 21 check: -1
2097152 trees of depth 4 check: -2097152
524288 trees of depth 6 check: -524288
131072 trees of depth 8 check: -131072
32768 trees of depth 10 check: -32768
8192 trees of depth 12 check: -8192
2048 trees of depth 14 check: -2048
512 trees of depth 16 check: -512
128 trees of depth 18 check: -128
32 trees of depth 20 check: -32
long lived tree of depth 20 check: -1

real 1m16.180s
user 0m0.000s
sys 0m0.046s

$ time binarytrees 20 (C#)
stretch tree of depth 21 check: -1
2097152 trees of depth 4 check: -2097152
524288 trees of depth 6 check: -524288
131072 trees of depth 8 check: -131072
32768 trees of depth 10 check: -32768
8192 trees of depth 12 check: -8192
2048 trees of depth 14 check: -2048
512 trees of depth 16 check: -512
128 trees of depth 18 check: -128
32 trees of depth 20 check: -32
long lived tree of depth 20 check: -1

real 0m54.618s
user 0m0.000s
sys 0m0.031s

$ time java -server -Xms64m binarytrees 20 (Java)
stretch tree of depth 21 check: -1
2097152 trees of depth 4 check: -2097152
524288 trees of depth 6 check: -524288
131072 trees of depth 8 check: -131072
32768 trees of depth 10 check: -32768
8192 trees of depth 12 check: -8192
2048 trees of depth 14 check: -2048
512 trees of depth 16 check: -512
128 trees of depth 18 check: -128
32 trees of depth 20 check: -32
long lived tree of depth 20 check: -1

real 0m36.328s
user 0m0.031s
sys 0m0.046s

Jun 27 '08 #202
Razii wrote:
On Thu, 05 Jun 2008 10:54:33 +0100, Jon Harrop <jo*@ffconsulta ncy.com>
wrote:
>>And Mersenne Twister, where F# is also 2x faster than Java.

It's empty claim since we have no chance to look at the code. The link
you gave was broken.
The link is fine:

http://www.math.sci.hiroshima-u.ac.j...JAVA/java.html
I wonder why Harpo doesn't post the link? Perhaps he knows the claim
will be debunked if we have a chance to optimize?
Try to optimize this one, for example:

http://www.math.sci.hiroshima-u.ac.j.../MTRandom.java

F# is 2x faster than this Java (and shorter, and more composable, and ...).

--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com/products/?u
Jun 27 '08 #203
Barry Kelly wrote:
Jon Harrop wrote:
>Jon Skeet [C# MVP] wrote:
Jon Harrop <jo*@ffconsulta ncy.comwrote:
That is Java outperforming C#, not .NET.
No, it's Java outperforming .NET calling a virtual method. It has
nothing to do with the code that the IL is generated in: if you have a
virtual method which isn't overridden, the .NET CLR will not inline
calls to it. The Hotspot VM will inline it until it first sees the
method being overridden. The difference here is in the jitting model,
not in the language.

Try it in F#, ensuring that the method is virtual and actually called
each time (consider that the method might be one which has side-
effects). I can't see how it would possibly have a different result.

This optimization can obviously be done statically by the compiler.

No - this optimization cannot be done statically for runtimes that
support dynamic code loading, as the CLR and JVM do...
I just posted a working example where the optimization is done, so it is
clearly not impossible. The only question is (subjectively) was my code
equivalent.

--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com/products/?u
Jun 27 '08 #204
Razii wrote:
On Thu, 05 Jun 2008 10:58:53 +0100, Jon Harrop <jo*@ffconsulta ncy.com>
wrote:
>>The fast .NET code is already on the shootout site (again).

I tested it and it's slowest.
Tested what, exactly? Where are your results? What is your setup? How did
you compile it? etc.

--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com/products/?u
Jun 27 '08 #205
On Thu, 05 Jun 2008 18:00:14 +0100, Jon Harrop <jo*@ffconsulta ncy.com>
wrote:
>F# is 2x faster than this Java (and shorter, and more composable, and ...).
Post the link to F# and C# versions.
Jun 27 '08 #206
On Thu, 05 Jun 2008 18:11:00 +0100, Jon Harrop <jo*@ffconsulta ncy.com>
wrote:
How did you compile it? etc.
fsc binarytreesf.fs

$ time binarytreesf 20
stretch tree of depth 21 check: -1
2097152 trees of depth 4 check: -2097152
524288 trees of depth 6 check: -524288
131072 trees of depth 8 check: -131072
32768 trees of depth 10 check: -32768
8192 trees of depth 12 check: -8192
2048 trees of depth 14 check: -2048
512 trees of depth 16 check: -512
128 trees of depth 18 check: -128
32 trees of depth 20 check: -32
long lived tree of depth 20 check: -1

real 1m16.180s
user 0m0.000s
sys 0m0.046s

that was slowest...

Jun 27 '08 #207
Razii wrote:
On Thu, 05 Jun 2008 10:59:58 +0100, Jon Harrop <jo*@ffconsulta ncy.com>
wrote:
>>Again, the fast .NET code (and compilation instructions) are already on
the shootout site. There is nothing for me to do (except tell you the same
thing over and over).

F# was much slower than C# and slowest of all...

$ time binarytreesf 20 (F#)
...
real 1m16.180s
user 0m0.000s
sys 0m0.046s
This takes 68s here.
$ time java -server -Xms64m binarytrees 20 (Java)
...
real 0m36.328s
user 0m0.031s
sys 0m0.046s
This takes 69s here.

Given that my CPU (2.2GHz Athlon64 X2) predates yours and is roughly half as
fast on other benchmarks overall, it is clear that your result for F# is
anomalous: taking longer on a much faster machine.

--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com/products/?u
Jun 27 '08 #208
Razii wrote:
On Thu, 05 Jun 2008 18:11:00 +0100, Jon Harrop <jo*@ffconsulta ncy.com>
wrote:
>How did you compile it? etc.

fsc binarytreesf.fs

$ time binarytreesf 20
stretch tree of depth 21 check: -1
2097152 trees of depth 4 check: -2097152
524288 trees of depth 6 check: -524288
131072 trees of depth 8 check: -131072
32768 trees of depth 10 check: -32768
8192 trees of depth 12 check: -8192
2048 trees of depth 14 check: -2048
512 trees of depth 16 check: -512
128 trees of depth 18 check: -128
32 trees of depth 20 check: -32
long lived tree of depth 20 check: -1

real 1m16.180s
user 0m0.000s
sys 0m0.046s

that was slowest...
Try inserting:

[<CompilationRep resentation(Com pilationReprese ntationFlags.Pe rmitNull)>]

before the line:

type 'a tree = ...

and then try compiling with optimizations enabled:

fsc -O3 binarytrees.fs

--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com/products/?u
Jun 27 '08 #209
Jon Skeet wrote:
Mark Thornton <mt*******@optr ak.co.ukwrote:
Pointless benchmark, of course, other than to show the inlining
capabilities of Hotspot in the face of a virtual method which hasn't
been overridden.
It does have real relevance. I have classes with multiple
implementations , but quite frequently a given process will only use one
of the possible implementations .

Fair enough - but it's only significant if:
a) the implementation is small enough to inline
b) the method call would be a significant hit, i.e. it's a bottleneck
in the app

Obviously I can't speak for your experience, but mine is certainly that
the two don't come together very often. More likely in the scientific
world than the business world, I suspect.
FWIW, when one adopts the test-first style of development, the ability
to create mocks (whether by inheritance or the proxy capabilities of the
CLR/etc.) and use dependency injection frameworks (inversion of control
pattern) to abstract away your dependencies becomes very important.

It's then that the ability of the runtime to optimize virtual method
calls to statically dispatched calls becomes a lot more important,
because:

1) Usually, the only point of a method being virtual is so that it can
easily be mocked

2) Usually, when running "for real" and not for test, only a single
implementation of that method will be loaded into the runtime.

I wouldn't discard the utility of the virtual -static call
optimization so trivially.

Of course, some kind of more direct language or runtime support for
mocking and / or inversion of control might be a better solution than
willy-nilly making every method virtual, as that has other, more severe
repercussions for designability and testability - classes with virtual
methods being a lot harder to test due to the extra code paths.

-- Barry

--
http://barrkel.blogspot.com/
Jun 27 '08 #210

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

80
3572
by: tech | last post by:
Hi, i have the following problem In file1.h namespace A { class Bar { void foo();
318
11198
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... http://kingrazi.blogspot.com/2008/05/shootout-c-net-vs-java-benchmarks.html Just to keep the post on topic for my friends at comp.lang.c++, how do I play default windows sounds with C++?
0
9791
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10845
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10412
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7970
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4215
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.