Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Does anyone know how many clock cycles it takes for a variable assignment in C language on a x86 platform? It is generally considered to take less when compared to java, what is the reason behind it?
The difference between C and Java is not depending on the difference between the languages themselves, but rather on the difference of technology behind those two languages:
C is compiled in binary, which is the code that gets directly executed by the processor. By opposition, Java is (most generally) never compiled totally, it is instead pseudo-compiled into bytecode.
This bytecode is designed to be interpreted by a virtual machine (the JVM in the case of Java), allowing for a much easier portability: while you need to adapt your C code to make it portable (see NetBSD for an example), or make different versions of it for each target; you just need a different JVM to run the same java bytecode on a different target.
It is worth noting that Java follows the JIT model, allowing for optimizations that are normally impossible, since they rely on conditions only known at run-time.
Now, in the case of your question, the real things to compare are: for a given machine, how much cycles does it take for a value to get copied in memory (RAM, even though that some C compilers can use CPU registers to store variables used a lot in a short time-span, like loop counters for example) with the assembly instruction versus how much cycles does the JVM take to do the same task upon reading the Java bytecode instruction to do so.
And I would say that with a good JVM implementation, there would be no difference for the allocation itself, as far as I understand. Now, there are other criteria to consider: Java usually makes heavy usage of objects, that take a lot of place in RAM, due to their complex nature, and therefore, take also more time to allocate. Also, I believe that Java makes more checks to avoid common mistakes, such as accessing a non-initialized variable, and those cost time too.
But keep in mind that a badly coded C program can take much more time to execute than a very well coded Java program.
Only after you understand this statement:
"Exactly zero lines of C code have ever been executed by a computer. Also, exactly zero lines of Java code have ever been executed by a computer."
You will never understand any answer to your question.
There answer to your question is:
"An assignment, written in C, takes an unknown number of clock cycles to complete."
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm fairly new to Java but love it so far. My question is, i'm a little unfamiliar with Classes. I know what they are, and generally how to work with them as I'm not brand new to programming, but I would like a professionals opinion.
I'm currently writing a small multi threading program to launch parallel power shell sessions by spawning cmdlines for target machines in a csv, capture the output and write to a csv.
Should I put everything into one class and breakup the logical operations to methods within the class and string them together? Or should I make a Thread executor class, cmdline powershell class, a csv operations class, etc (My thought behind that was to allow code reuse, but that'll be kindove time consuming and in my mind impractical since i'd have to specify the datatypes and return types for new situations in the future).
Any help would be appreciated!
There is no "way" so to speak,
It's all your preference.
But just don't cram everything into one class.
Generally, you want to be as neat as possible.
In the future, you will thank yourself for using different classes.
If your project grows, and a bug is born, you don't want to be looking through one very long class, but instead simple broken up pieces.
Let's say you have these classes:
GPS,
Main,
Search
And someone reports a bug with the GPS not working.
Instead of looking everywhere saying, where did I put the GPS code,
it's right in front of your eyes!
I've went to everyones links and found the info very helpful. So far I've come up with this.
Make a package that contains classes that perform a specific set of tasks (also don't make utility kits that are very general). The package in my case would be called com.jt.threads.powershell or something.
Keep classes small and breakup the program by conceptual types. (ie. data reading and writing operations on a filesystem should be in one class with the focus on helping the package perform a certain task or range of tasks.)
Methods within classes should focus on getting, setting, changing the objects attributes or adding logic.
The program entry point should join it all together, except in the case of large applications, in which case an interface should be used (still learning about them).
With true OOP, i don't think it's a good idea to create code for reuse, unless it's supporting a range of very very very similar tasks (that way if I have to change something, it won't break other classes outside of the package).
Thank you all! I feel a lot better knowing that I'm on the right track. I was worried that by NOT making code reusable in a lot of situations that I was doing something wrong. I started programming in Python 6 months ago for my job, but I totally ignored classes and I want to have good programming habits and apply OOP as best I can going forward! Python is definitely convenient and a great starter language, but I wish I learnt Java first so I can get a solid grasp on OOP.
There is no “The way” to organize or group classes. Anything goes as long as it works as expected and you understand what you write.
As a Programmer you only need to,
1. Know and understand what you write.
2. Know and understand what other Programmer as written.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Background
On every large, commercial Java project I've worked on, I come across numerous usages of Pattern.compile(...) even in code segments which are re-used many times, e.g.
public String rewriteUrlWhichIsDoneABajillionTimes(final String requestedUrl) {
Matcher m = Pattern.compile("^/([^/]+)\\.html$").matcher(requestedUrl);
if (!m.matches()) {
return null;
}
// Do processing here
...
}
For every project on which I found things like this, I told at least one person whom I was working with that Pattern.compile(...) is very slow and is not cached but that the java.util.regex.Pattern class is thread-safe and so it can be safely re-used, and each time they informed me that they did not know these things.
Potential solutions
Correct future usage of the API
One "solution" could be to (try to) force people to read the Java standard library documentation and to use the standard library "correctly", but prescriptive methods often to not work so well.
Correct past usage of the API
Alternatively (or complementarily), it would be possible to "clean up" any bad usages of Pattern.compile(...) wherever they are found, but this is likely to be a never-ending task, since (according to my experience) people will continue to use Pattern.compile(...) incorrectly over and over again...
Correct the API
So why not then simply change the Pattern.compile(...) method so that it pools objects and returns the same instance for equivalent input?-- this would instantaneously apply a fix to possibly billions of lines of code around the world (as long as the respective code is run using a JRE which includes the change). The only possible downside I can imagine is that the software would have a larger memory footprint... but given how much memory most computers have these days, I doubt that this will cause problems anywhere other than in edge cases. On the other hand, a huge number of programs will likely run much faster. So why didn't/doesn't Oracle implement an object pool for Pattern similarly to how they did for strings or for primitives?
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
In most cases, is it better to reverse an integer by extracting digits and putting them onto a string and use valueOf() to obtain the value; or to reverse the integer directly as an integer?
I'm inclined to say the former is better due to it being ubiquitous in the answer solutions of my book whenever they need to reverse an integer.
What is the drawback of using the other approach besides being a bit more time-consuming to write and read?
The first way (converting the integer to a string, reversing the string, then converting it back):
Is easier to understand.
Is likely to require less code.
Is likely to be slower.
The second way:
Is harder to understand.
Is likely to require more code.
Is likely to be faster (for one thing, it requires no memory accesses).
Of course, modern computers are so complex that the only way to tell whether something is faster is to actually measure it. I said the second way is likely to be faster, but that's just a guess.
Conclusion:
If you need the code to be written quickly, do the first one because it's easier.
If you need the code to run quickly, measure how long each one takes to run, and use whichever one is faster.
But, in practice, apart from programming exercises, you will never need to reverse an integer. So if you're asking which algorithm is used more often in the real world, the answer is neither.
The String method is easier to write and more readable by other programmers (and yourself in 2 weeks). The direct conversion method using modulo is faster and uses less memory but it's a bit harder to read and write.
Note that with JIT compilation and the improvements in JVM in general, the first method could be internally optimized to closely mimic the latter. And we all know that a general rule of programming is to avoid "premature optimization". Thus the more readable method using String is probably best.
What do you mean "better" and what do you mean "for general purpose"?
For the general purpose of explaining how to code in a textbook, a for loop and modulus is best.
For the general purpose of being clearly correct, I would suggest converting to string since this is a string operation, not an integer operation.
I haven't seen this operation in production anywhere so I can't attest what anyone actually does with it. I think this was a trick question - your "general purpose" may not exist after all.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Right now, I'm developing a heavy 2D game write in java+LWJGL (ok, it could have looked funny when I said "heavy 2D game", but trust me, I'll be using LOTS of resources), and, well, let's say I have TOC (TOC is in Brazil, maybe in english it's... OCD?), so I have to write, for everything I'll use third-party, an API. Like, I have an API for LWJGL (an API for an API of OpenGL, lol), and a lot of other API classes. And, inside these APIs, on constructors, I never use primitives (constructors only, fields I still use primitives), but instead I use Number class (so I can call the methods using Integer, Float, Double, and whatever I want). Then, inside the constructor, I translate the number, using number.doubleValue() or number.intValue(), depending on what I want.
So, do this can affect dramatically the performance of my game? Until now, in the early-middle development stage, my performance is still OK but I'm worried of it causing me to re-write most part of the code late. Oh, and sorry for my soo bad english, it's not my primary language.
Don't use boxed primitives unless you really need them. A good reasons may be
you need an representation for the undefined state (use null)
you put them into Object-accepting classes (List, Set, Map, ...) and want to avoid boxing (by working with boxed values all the time)
Anything performance-related must be benchmarked first, otherwise you can find yourself optimizing a piece of program taking no measurable time or even "pessimizing" it. Low-level optimizations in Java are pretty hard, so you'd better concentrate on clarity and readability, so you can measure the speed, identify the bottlenecks, and optimize them afterwards.
Concerning boxed primitives, IMHO the biggest performance impact doesn't come from object creation but from the indirection (creation happens once, repeated cache misses are costly).
I disagree with dognose concerning the "fancy OOP stuff". Everything the classes offer can be used without boxing and working with boxed primitives is a pain the OOP methods can't ever make good (the unexpected NullPointerExceptions, non-working conversions, etc. are IMHO too bad).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
First off all, I know there are several questions about "Java inline". But they are all about how the compiler or JVM inlines function calls. I'm interested in doing this myself, or create some kind of a View for it. I want to define a function call of a class, and want to see everything inlined. Every method call should get inlined. I'm not sure how to handle instantiation of new objects, but it doesn't matter as much.
The goal is manual optimization, i.e. if a parameter is checked too often against null. Is there a tool to to something like this? I would prefer a GUI, but some kind of command line tool where I can specify a class function and it dumps some text somewhere will suffice, too.
EDIT:
For clearification:
Today I argued to use the NullObjectPattern, because some are defensively overchecking for nulls everywhere. This makes the code unreadable and unclean. I dont like it and wanted to have some kind of a tool, to show them how often they are actually checking the very same parameter again and again for null.
As was said: Don't guess, especially when you don't know what the JIT compiler will do after the code has been running for a while. You can waste infinite time infinitely improving something that accounts for 1% of runtime and only save 1%, or you can spend a short time getting a 10% improvement of something that accounts for 20% of your runtime and save 2%; the latter is by far a better choice.
The way you determine what's worth improving is by properly profiling your code after it has been fully warmed up.
And the way you get a significant improvement generally has more to do with improved algorithms than with microtuning of single instructions.