473,769 Members | 6,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ vs Java "new" (no flame war please!)

mlw
Do not take anything about this, it is not a flame or troll, while I'm not
new to Java I favor C++. However, I may need to use it in a contract
position, and am concerned that the restrictions it places on application
code.

Take, for instance, this C++ construct:

class foo
{
char *m_name;
....
void * operator new(size_t size, char *string);
}
void *foo::new(size_ t size, char *string)
{
size_t cbstr = strlen(string)+ 1;
size_t cb = cbstr + size;
foo * t = (foo *) malloc(cb);
char * name = (char *) &t[1];
strcpy(name, string);
t->m_name = name;
}

The above example is a methodology that can be used to reduce the CPU and
memory overhead of malloc. You my argue that this is not a valid concern,
but if you have 10 or 100 million objects, the malloc block overhead,
alone, make this worth while. Hint: This is actually a simplification, some
times malloc is not used at all, and a big array is pre-alocated and work
through it with each new.
Is there a way to create 10 to 100 million objects in Java with a reasonable
system configuration?

Apr 1 '07
14 5692
mlw
Lew wrote:
mlw wrote:
>real 0m1.020s
user 0m0.860s
sys 0m0.092s
test@localhost :~/scat$ time java test
Java heap space
Total object:741838

real 0m19.178s
user 0m16.893s
sys 0m1.728s

You're still not timing memory allocation but JVM startup and other
factors.
Start up is NOT 18 seconds, so what other factors could we possibly be
talking about?
Apr 2 '07 #11
>>>Is there a way to create 10 to 100 million objects in Java with a
>>>reasonable system configuration?
>Mark Rafn wrote:
>Any VM since 1.4 on modern hardware should not have a problem with this,
unless you're pretty time-sensitive.
mlw <ml*@nospamnowa y.zzwrote:
>To address the "time-sensitive" comment, many times I hear that the machines
are fast enough that you don't need to worry about performance,
I don't think I'd ever say that. I will say that machines are fast and cheap
enough that for a whole lot of uses you can optimize for clarity,
maintainability , and developer time over every last cycle of performance.
It's definitely still a tradeoff, just one that's shifted quite a ways.
>I have to say this is the same argument for the 4 day work week. "Soon,
we'll be productive enough that we'll only have to work 4 days a week."
I'm productive enough to earn far more than 125% of what I did a few years
ago. No myth here.
>If you make a program that takes an hour to do something, and someone comes
along and writes one that takes 5 minutes, you lose, no matter what the
program is written in.
Depends on the program. I have programs that run for an hour each day, that
could probably be cut down to 30 minutes (the difference would be changes in
DB usage and transactional consistency, not just reimplement in a different
language), but I have better things to worry about.

The VAST majority of programs aren't 12:1 difference. If you write a program
that takes 11 seconds to do something, and someone comes along and writes one
that does it in 10.6, but only on some platforms and in a hard-to-maintain
way, I think they lose.
--
Mark Rafn da***@dagon.net <http://www.dagon.net/>
Apr 3 '07 #12
Lew
mlw wrote:
Lew wrote:
>mlw wrote:
>>real 0m1.020s
user 0m0.860s
sys 0m0.092s
test@localhos t:~/scat$ time java test
Java heap space
Total object:741838

real 0m19.178s
user 0m16.893s
sys 0m1.728s
You're still not timing memory allocation but JVM startup and other
factors.

Start up is NOT 18 seconds, so what other factors could we possibly be
talking about?
Beats me.

-- Lew
Apr 3 '07 #13
mlw wrote:
Is there a way to create 10 to 100 million objects in Java with a reasonable
system configuration?
A question: this routine and the 10/100 million objects refer to a
real-world case or are just speculation?
What exactly the application do, in the overall?

I think that many of the speed/size speculation around are just applied
to micro-benchmarks, and that there is an absolute lack of real-world
benchmarks (entire applications converted from one language to another).

Micro-benchmarks haven't any real scientific value. But they are still
diffused because they're easy to develop.
It's very curious to me the fact that nobody would think about comparing
two F1-cars just by benchmarking their tires (!?), but the same approach
with programming languages implementations is commonly conceived as
perfectly reasonable and accurate by the programming community.

Quake 2 has been written in C by a absolute-history programming master
(John Carmack), in C and assembler.
Jake 2, the Java conversion, is 85% faster. This is impressive.

I stress that this is the only real-world benchmark which I've EVER
seen, and I would be EXTREMELY interested in real-world benchmarks, but
I still can't found any of them.

The entire story remembers me of an article written by Tom Miller:

http://msdn.microsoft.com/msdnmag/is...t/default.aspx

Bye!
IM
Jun 30 '07 #14
Lew wrote:
mlw wrote:
>Lew wrote:
>>mlw wrote:
real 0m1.020s
user 0m0.860s
sys 0m0.092s
test@localho st:~/scat$ time java test
Java heap space
Total object:741838

real 0m19.178s
user 0m16.893s
sys 0m1.728s
You're still not timing memory allocation but JVM startup and other
factors.

Start up is NOT 18 seconds, so what other factors could we possibly be
talking about?

Beats me.

-- Lew
Actually it doesn't beat you. Instead, it beats people which have enough
inexperience in a matter, to publish microbenchmarks in that specific
matter.

First law of microbenchmarks : microbenchmarks are flawed and misleading
by definition.
Corollary: Heisenbenchmark principle.

The following code demonstrate one flaw of the presented MB, regarding
the speed analysis.
There is [at least] another big flaw in the comparison, guess which.

Also, if we want to play the "speed freaks" game, it is possible, via
some simple and clean optimizations, to obtain about 5x speed and 1.5x
objects total at the same time.

/* --- */

class foo {
private String m_test;
foo(String value) { m_test = value; }
void print() { System.out.prin tln(m_test); }
};

public class LargeAllocation {
private static final int TOT_OBJS =
2000 * 1000; /* takes 8 secs for 914733 objects,
after which the JVM explodes */
// 900 * 1000; // FLAW: should take few less than 8 secs, right?

private static long start, end;

public static void main(String[] args) {
start = System.currentT imeMillis();

allocate(TOT_OB JS);

end = System.currentT imeMillis();
System.out.prin tln("Time: " + (end - start));
}

private static void allocate(int numObjs) {
int i = 0;
try {
foo arr[] = new foo[numObjs];
for (i = 0; i < numObjs; i++)
arr[i] = new foo(Integer.toH exString(i));
}
catch (OutOfMemoryErr or e) {
System.out.prin tln("Error: " + e.getMessage()) ;
System.out.prin tln("Total objects: " + i);
}
}
};

Bye!
IM
Jun 30 '07 #15

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

Similar topics

1
3191
by: novel | last post by:
Hi, i wrote a java program, that create a rdf file parsing a file tree. On the last step the shockwaves and gifs are checked and the byte sizes are written in the rdf file as nodes. The 1. Problem: A java.lang.OutOfMemoryError is throwm. Sometimes on the 45 shockwave, sometimes on the 53 shockwave...
4
2859
by: SCOTT DELIBAC | last post by:
Can Anyone Help?? I have this program which is listed below. I want to take the grades that the students have assigned to them, and assign the letter grades a numeric value so that I can calculate their GPA. I just have no idea how I am supposed to do that?? Can anyone HELP!!! import javax.swing.*; import java.util.*; public class GradePoint { public static void main(String args) {
2
1360
by: Mondo Dynamo | last post by:
A friend of mine is learning java as part of a Business Studies Degree and is finding it very hard to get to grips with. She has an assignment coming up soon and needs some help, she is offering a pretty unique reward, if you are interested please visit http://www.mondodynamo.com/hazeljava.htm Thanks in advance for any help you can offer. --
16
1691
by: codemonk | last post by:
Hi all, Is there anyone else on this board that feels Extreme Programming (although) making some great points is a little overcooked, extreme and just plain nonsense at times? Stede
3
3406
by: andy | last post by:
I am starting a new application, it needs to use db such as access or msde. I know I can do this easily in c# or Java but my question is can I distribute a package as easy if it is created with java and say derby db as I can with windows based. It needs to be click to install and thats it, no exceptions. To date I have done this with vb and c++ with access and have had excellent success. I really want to make the transistion to java...
1
1648
by: Vishuonline | last post by:
Guys, I have had some experience in working with webservices created in .NET but this is the first time I am trying out one made in Java. When I run the wsdl exe to create a proxy class for the wsdl provided it throws the following error. Error: Unable to import binding 'CosmosXXXXXBinding' from namespace 'urn :XXXXX'.
3
1470
by: siddhu085 | last post by:
hi! i'm new to java script. i'm doing a web page, where i can increment the value of a text field by pressing a button. Each time i press the button, the value in the text field should be incremented i wrote a code for this and it returned an error Line: 9 Char: 2 Error: Object Expected Code: 0
1
1206
by: kprojects | last post by:
Hello, Im creating an applet in which the user has to log in, and after he did this with the correct login details he will be redirected to a java page with his account details (Just another page in the applet). Because this is my very first Big applet i have no idea what's the easiest way to do this. I thought about setting every variable's visibility false but that would be a lot of work. If you guys know an easier way, please let me know!...
4
1172
by: =?Utf-8?B?Q2hhcmxlcw==?= | last post by:
Hello all, I'm trying to convert a Java Genetic Programming program to C#. I don't know how to convert the following Java code: Object choice = functionSet.getSelectedItem(choice)).value(); Class cls = ((ProgramChoice) choice; function = (Function) cls.newInstance(); The object "choice" shows that a Subtraction class (i.e. "sub") was selected,
2
3961
by: lilyumestar | last post by:
This project is due by Tuesday and I haven't even gotten half of it done. Can anyone please help me with this Exception error? I've been trying to figure it out for several hours Error Message "Main" Java.lang NullPointerException at Project1.sortingByZipCode<Project1.java:80> at Project1.main<Project1.java:31> Here is the Source Code
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10214
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
10048
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
9996
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
9865
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
8872
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...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3963
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
3563
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.