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 7 years ago.
Improve this question
Hey guys,
I'm a relatively new programmer in Java(and in general), but I want to know different ways of minimizing memory and RAM usage in programs that I make. I've heard of a few such as StringBuilder as an alternative to + String concatenation and stuff, but I'd like to hear what you guys know about how to maximize performance and why.
Thanks in advance!
In modern programming, it is a far better use of time your time to focus on making your code readable rather than trying to micro-optimse.
Modern compilers do an extremely impressive job of these small optimisations so that everyday programmers don't have to deal with them, and in the majority of cases it is better to leave it up to the compiler than to attempt it yourself.
In general I would say that the largest performance improvements can be gained by thinking about the design of your program ahead of time, before you even start typing. Once you've already bashed out 10,000+ lines of code implementing your latest 3D high-performance MMORPG, and you realise it's not as high-performance as you were hoping, making any drastic design changes will be considerable work. Some things to think about beforehand are:
Think about your algorithms complexity, for example string concatenation can be O(n^2) using String, but O(n) using StringBuilder.
Use object pools to reuse memory rather than creating new instances each time
Reuse existing library implementations of data-structures etc, rather than trying to recreate them yourself. Many more man-hours will have been put into these implementations than you could possibly spend on them, and so they are likely to be more efficient/robust
Finally I should mention, that if you do go trying to optimise some existing code because its not performing as well as needed, it's very important to know specifically where the problem area is. In this case a profiler is invaluable, and should help pinpoint any particular areas that are affecting performance. They might not be where you expect!
Related
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 6 years ago.
Improve this question
Hello stackoverflow i have a doubt, which is the proper way to solve this?
I have this sql sentence:
SELECT *
FROM task
, subject
WHERE task.id_subject = task.id
AND task.id_tasktype = 1
AND subject.id_evaluation = {ALL the ids of table evaluation}
If i want to execute this sentence for every evaluation what is more efficient? a loop/cursor or whatever in SQL (i have basic knowledge of sql) or a regular for each in Java?
It depends on your situation. Basically, if your database server and application server are actually two different computers, then you might decide to run the loop at the server which can handle more pressure. You need to look at some statistics to be able to determine this.
Also, you can implement both solutions and measure the time needed at db server + time needed at application server. If one of your loops is consistently quicker than the other, then it is practically more efficient in the scenario you are running it according to your experiments. Off course, the scenario might change over time.
Generally speaking, people tend to run this loop on the application server (Java), since you might need to execute some things available only there in the future, but if you have a very good reason to run this on the database server, like the case when a trigger should trigger this functionality, then you might decide to run it there.
Basically, you are trying to optimize a loop where you do not necessarily have a problem. If you encounter performance issues, then you might decide to experiment with a few things, including, but not limited to the suggestions shown in your question.
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
I had been reading two books on JAVA and while covering data structures, I started to do some online research with regards to "QUEUE" implementation. I have an extensive background in FLEX, however ACTIONSCRIPT isn't comparable to advance languages.
Lets say if I was on a job interview and asked to implement a Queue of Object, how should I pursue it as? I am not looking for code help here, I would like to what would you quick answer be? I have been to Java online docs and do understand there are 13 known implementing classes, and "LinkedList" is one of them.
Google search has return more results with "LinkedList" implementation code than any other.
My apologies if you find this question to be rubbish or pointless in anyway.
Oracle's Java online doc ref:
Do you know what the concept of a queue is and how it differs from a stack (closely related data structure)? If so, you should be able to think of multiple ways to implement it.
Which is best depends on the exact requirements of the task it's being used to address.
So the right response to that interview question is not to start coding but to ask them for more information about the requirements your implementation has to address. Performance? Memory size? Multitasking? Any limits on maximum queue depth, eg to guard against things like a DOS attack? What's being enqueued -- objects, primitives, other? Specific kinds thereof? Parameterized type? Are there any values which should be discarded (maybe null shouldn't be enqueued)?
Knowing the requirements, you should be able to judge which answer is appropriate. Starting coding without asking the requirements is immediately going to earn you a demerit.
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 9 years ago.
Improve this question
Ok I gave been playing around with java for a year now an I can say that is is in my power to write a fully functional program.
A month ago I started studying vectors and so, when I tempted to implementing them into the LWJGL I realized that Java is not fast enough for the level of graphics that I wish to generate.
Now that is my problem and I have decided that I must learn a stronger Language but where do I begin I have tinkered around in C/C++ before but it kills my ambition to go and start over after already using a hole year.
my "algors" for rendering a vector are:
z = r(cos t+j sin t) //where the t is degrees and r is its length
(for the curious)
the program the continues a loop that alters its length with 1, gets its end X and Y and draws a pixel on that spot.
Not for game coding.
Name 1 free E-Book that will get Me on My feet with C
I realized that Java is not fast enough for the level of graphics that I wish to generate.
Java is plenty fast. Unless you want to work with high performance graphics, you can use it safely (and you will probably be able to use it even with high performance graphics).
In the vast majority of cases, the speed of your application will be a function of algorithmic complexity, not language used.
After your application is completed, if it doesn't run fast enough, you can optimize. If it still doesn't run fast enough, you can implement critical parts in C/C++/your-language-here.
If you start from "the language is not fast enough", you're already doing it wrong.
If you want to write a game, use an engine. There are many great engines in many languages. If you wonder about language-inherent performance, and you try to solve technological issues yourself, you are gonna be stuck doing that forever.
Do not reinvent the wheel. Stop worrying about which programming language is the best fit, and rather think about which engine is best suited for what you want to make.
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 9 years ago.
Improve this question
This is kind of unusual question for developers but for some reason i want to post it here and hope to get adequate answer.
Here is a simple example:
I wrote a java function that calculates distance between two geo points. The function is not more than 50 lines of code. I decided to download a source code from ibm that does the same thing but when i opened it i saw that it looks very complicated and is almost thousand lines of code.
What kind of people write such source code? Are they just very good programmers? Should i use their source code or my own?
I have noticed this kind of thing lots of times and i from time to time i start to wonder if it is just me who do not know how exactly to program or maybe i am wrong?
Do you guys have the same kind of feeling when you browse throught some other peoples source code?
The code you found, does it do the exact same calculation? Perhaps it takes into account some edge cases you didn't think of, or uses an algorithm that has better numerical stability, lower asymptotic complexity, or is written to take advantage of branch prediction or CPU caches. Or it could be just over-engineered.
Remember the saying: "For every complex problem there is a solution that is simple, elegant, and wrong." If you are dealing with numerical software, even the most basic problems like adding a bunch of numbers can turn out to be surprisingly complex.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I'm looking for a tool to count source lines of code for Java as well as giving an estimate of the number of man-years invested in the code. Since Java code tends to be more verbose than other languages, with a lot of boilerplate code (anemic beans) generated by the IDE, I want the tool's metric to take this into account.
If someone can just provide the formula to convert source line count to man years (for java), this is also good.
This sounds like a really bad idea.
The best way to estimate the number of man years work on a piece of code it to look at who worked on it and how long.
Trying to infer this man years from SLOC is likely to be highly inaccurate and misleading. For example:
At some point in the software lifecycle many lines of code can be added. In some periods of maintenance / refactoring code may be actually taken away.
Code that has had a lot of requirements changes and quick hacks is likely to have more SLOC than equivalent code that was cleanly designed and written in the first place.
The same functionality can be written with 100 lines or 1000 lines depending on the libraries / frameworks used.
Are you going to count SLOC in libraries too? What about the JVM? What about the underlying OS?
In short, any estimate of man years derived from SLOC is likely to be pretty meaningless.
Although you want the information for bad purposes SLOC is a nice, easy, not very useful metric. Make sure you read this older conversation first
One of my most productive days was throwing away 1000 lines of
code.(Kent beck).
It is not going to be accurate for various reasons. Some from my experience ..
Code gets added , changed or deleted: If you really want query your
SCM for change history and then map to changed lines.
Architectural changes/Introducing a library replacing your code. : In
our case it reduced Coding only part of the change: Design
discussions, client interactions, documentation etc will not be
reflected in code, even though I consider they are development effort
Finally developers are f varying productivity (1 : 40 , some said):
How are you going to map into developer time?
SLOC is a useful tool to say my code base it 'this large' or 'this small'..
Looks like http://www.dwheeler.com/sloccount/ is the best bet.
at the office i use ProjectCodeMeter to estimate man-years invested in a source code, it's kind of a luxury tool at that price, but i did use the free trial version at home on occasions :)