# Possible to force my java app to use more CPU utilization?



## grecinos (Jan 13, 2017)

Hey folks,

I've been developing a Java applet that requires significant CPU utilization.  For the moment, I'm only able to run it from the IDE, Java NetBeans.  I've been having issues getting it to work on a web browser due to security issues.  Nevertheless, when I run the app, I noticed that the Task Manager shows about 20% CPU utilization.  Is this a limitation of the OS (Windows 10, 64-bit)?  I'd like for it to utilize at least 70% of the CPU'S capability, if not more.  Is there a way to force my app to utilize the CPU to that extent? 

The laptop PC's I've been running the app on have Intel CPU's (i7-4720HQ, 3610QM) with 24GB and 32 GB of RAM respectively.  They both have the latest Java JDK/JRE installed.

Any and all advice is greatly appreciated.

Cheers.


----------



## R-T-B (Jan 13, 2017)

You are probably using all of one core of a quad core.  You'll need to multithread your app to get more performance.  This is not necessarily easy.


----------



## Capitan Harlock (Jan 13, 2017)

Hi , you can try to set the cpu priorities to real time and set the affinity too and lower the ones of other applications.

Edit: inside taskmanager.


----------



## newtekie1 (Jan 13, 2017)

I don't even thinkg Java apps can be multi-threaded.  If they could, I'd think programs like Minecraft would have done it long ago, but they haven't.


----------



## FordGT90Concept (Jan 13, 2017)

http://www.tutorialspoint.com/java/java_multithreading.htm


----------



## grecinos (Jan 14, 2017)

Thanks for the quick responses.

Captain Harlock:  I was able to modify the CPU priorities and affinity.  But there wasn't any change in the CPU utilization.

Ford:  Thanks for the link, there's a lot of information in that article, but at first glance, I don't think it said anything with respect to CPU utilization across all cores.

I was hoping that there would be a compiler directive that enables the use of all cores on the CPU.  I've already enabled the directive to compile the code to 64-bit mode (for lack of better terminology).

Any other ideas?


----------



## LFaWolf (Jan 14, 2017)

There is no single compiler directive or library that can instantly turn your app into multi-threaded, because that would be too easy  

Are you making a Java application (jar)? To make any multi-threaded application, you really need to think carefully and structure your code so that the compiler/runtime knows certain sections of code or method calls can be done asynchronously, in C# such as Task or async. Otherwise, the runtime will not know where to hand off computation to a different thread. Of course, with modern compilers there will be built in improvement to speed up certain tasks using multiple threads, but to make your code a multi-threaded app, you will need to do it yourself.

Most applications are fine running single thread, such as Word or Powerpoint. Multi-threaded apps are suited for computation intensive apps such as scientific or gaming. Few apps need that hence developers general don't spend time doing it, as the task is not easy. That is also why you see some games are poorly optimized to run on multi-core processors. What app are you making require so much computation?


----------



## Aquinus (Jan 14, 2017)

Java doesn't just automatically make your code run concurrently. You need to tell Java that and you need to do extra work to make sure that it remains thread safe and a lot of times, people who don't know what they're doing, won't see any performance benefit because you can't just throw more threads and more cores at a problem and expect everything to work out. I personally don't really write Java very often anymore but, I do write a lot of Clojure which runs on top of the JVM and I can say that it's very capable of utilizing a quad-core CPU.

Whenever you decide to make an application multi-threaded, there are a lot of questions you need to be asking yourself because there is always a trade-off with using additional threads.

So, if you want us to help you think through what parts of your application can be made to be concurrent and yield the most likely case of improving performance, then you need to tell us a bit about the problem you're trying to solve, otherwise this thread ends right here because as someone who writes multi-threaded code that runs on the JVM, I can't help if I don't know what the task is.


----------



## grecinos (Jan 14, 2017)

I've been away from Java for a while, so my code is not the most efficient.  Nevertheless, like I stated in my first post, It's a Java Applet.  The purpose is to make interesting linear designs through animating individual points.  I put it in an infinite update/repaint loop, as per the conventional method to make applets animate and execute code to handle updates with each iteration.  I suppose you could say that is the primary thread.  Every loop, it recalculates the new position of the individual points, and draws them to an image buffer.  I use double buffering to make the animation appear smooth.  The other half of the app is the UI.  I used Netbeans to create the UI  (a JFrame) that the applet instantiates.  The applet has a timer that executes code every 10 ms.  Every 10 ms, the applet checks the UI for state changes in the UI controls and performs the appropriate action.  It has a bunch of Jbuttons, Jsliders, Jlist boxes, etc.

That chunk of code looks like this:

 Timer timer = new Timer(10, new ActionListener()
  {          
            @Override
            public void actionPerformed(ActionEvent e)
            {  
                //User input handling goes here...
            }
   });
   timer.setRepeats(true);
   timer.start();

I used a Timer because it was the simplest to implement.  There's likely a better way to do this, but it works just as I had envisioned.  Lastly, the meat of the code is in calculating the position and colors of the individual pixels.  I'm using a lot of mathematical functions like sin, cos, etc.  This is where most of the CPU spends it's time calculating.  That's pretty much it.  Simple in design.  My objective, for the moment,  is to get this app to utilize the CPU to the extent of it's capability.  I don't care if that means multi-threading or code optimization.  I guess you can say, whatever works without mangling the code...


----------



## Aquinus (Jan 14, 2017)

grecinos said:


> I guess you can say, whatever works without mangling the code.


Then stop right now because you'll need to mangle that calculation of pixel colors and positions to multi-thread it in a useful way, assuming it's not a calculation that also has some sort of effect. There is no switch to use the rest of your CPU. 

In all seriousness, you should use a queue in front of and after the calculations with sin, cos, etc. so you can scale the number of workers doing strict math to the resources available on the machine being used. Since these workers are merely processing data (which means, no side-effects,) then the threads can merely stick around and you just feed a queue and pull from the queue (you could even use a thread pool.) Just keep in mind there is coordination overhead that needs to be factored in and you can't do this by not changing your code, probably significantly.



grecinos said:


> Java Applet


No one uses applets anymore.


----------



## grecinos (Jan 14, 2017)

Aquinus said:


> Then stop right now because you'll need to mangle that calculation of pixel colors and positions to multi-thread it in a useful way, assuming it's not a calculation that also has some sort of effect. There is no switch to use the rest of your CPU.



Yea, I think the added complexity isn't worth improvement in performance.



Aquinus said:


> No one uses applets anymore.



I'm considering porting the applet code to a standard Java app.  As I've mentioned in the first post, the primary reason I wanted to create an applet was to be able to have it run in a web browser.  Unfortunately, I'm finding it more and more difficult to get it to work due to security issues.  Does animation work just as well with a JFrame?  If so, what combination of Swing Containers/Controls/Windows works best?


----------



## FordGT90Concept (Jan 14, 2017)

Since your focus is web, perhaps you should look into HTML5 animation instead:
http://www.html5canvastutorials.com/advanced/html5-canvas-animation-stage/

Pretty sure browsers run animations in the GPU so you're killing two birds with one stone (no more security issues and should vastly improve draw performance).


If you demand to stick to Java, is each frame static?  Do you know the inputs and outputs of frame 2 and 3 while working on frame 1?  If yes and yes, you could create a function which draws each frame given the frame number and have the number of processors minus one (for main thread) drawing a frame.  Have the main thread pull from the queue of finished frames at a regular interval and order the thread that it took the frame from to work on the next frame in the queue.  In a quad core processor (three workers), for example, it would wait for frame 1 from worker 1 then worker would start on frame 4 while the main thread draws frame 2 from worker 2 (so on and so fourth).


----------



## Aquinus (Jan 14, 2017)

FordGT90Concept said:


> Since your focus is web, perhaps you should look into HTML5 animation instead:
> http://www.html5canvastutorials.com/advanced/html5-canvas-animation-stage/
> 
> Pretty sure browsers run animations in the GPU so you're killing two birds with one stone (no more security issues and should vastly improve draw performance).


I was just about to suggest this. Good advice.


----------



## R-T-B (Jan 14, 2017)

newtekie1 said:


> I don't even thinkg Java apps can be multi-threaded.  If they could, I'd think programs like Minecraft would have done it long ago, but they haven't.



Ironically, Java is one of the most multithreading friendly languages around.  Minecraft is just a coders nightmare.


----------



## Aquinus (Jan 14, 2017)

R-T-B said:


> Ironically, Java is one of the most multithreading friendly languages around.  Minecraft is just a coders nightmare.


This in the sense that it has some of the best facilities for doing it but, I wouldn't call it easy in the sense that locking is the only way to handle controlled mutation of a thread-safe reference. Clojure, being a functional lisp on that runs on the JVM makes multi-threading a piece of cake and it uses the JVM for that reason. A dynamic language that runs on the JVM and can run almost as fast as native java, is freaking awesome. I use it and evangelize it for a reason.

https://benchmarksgame.alioth.debian.org/u64q/clojure.html
Disclaimer: The benchmark game is both a benchmark *and* a game.

I'm not fanatical about many things but, Clojure is one of them.


----------



## grecinos (Jan 15, 2017)

FordGT90Concept said:


> Since your focus is web, perhaps you should look into HTML5 animation instead:
> http://www.html5canvastutorials.com/advanced/html5-canvas-animation-stage/
> 
> Pretty sure browsers run animations in the GPU so you're killing two birds with one stone (no more security issues and should vastly improve draw performance).



This looks interesting.  How does this compare to Java in terms of speed?



FordGT90Concept said:


> If you demand to stick to Java, is each frame static?  Do you know the inputs and outputs of frame 2 and 3 while working on frame 1?  If yes and yes, you could create a function which draws each frame given the frame number and have the number of processors minus one (for main thread) drawing a frame.  Have the main thread pull from the queue of finished frames at a regular interval and order the thread that it took the frame from to work on the next frame in the queue.  In a quad core processor (three workers), for example, it would wait for frame 1 from worker 1 then worker would start on frame 4 while the main thread draws frame 2 from worker 2 (so on and so fourth).



My main class is a stand-alone applet that instantiates a Jframe for user controls.  I can access the properties and controls this way.  Ideally, I'd like to create another Jframe as a "drawing canvas" where the animation will take place.  I've yet to figure this out.  I'm assuming the drawing canvas will require a Swing JPanel and perhaps a JLabel.  My best guess is that I will need to update the JLabel with the graphics I wish to display/animate.


----------



## grecinos (Jan 16, 2017)

Well... I decided to port My applet to a Java App.  I took the 1% inspiration, 99% perspiration approach.  After a significant amount of research, I was able to do it.  No mangling of the code, just well thought out adjustments.  So now, I can run the app from the command-line, whereas before, I was bound to the IDE.  Cool stuff.  In terms of speed improvement, initial tests show about the same.  Perhaps 1 or 2 fps better, but not much more.

Here are some other observations.  When I run my app, it uses about 20% CPU utilization.  When I spawn a second or third instance of the app, each instance also uses about 20% CPU utilization.  Since java is multi platform compatible, I'm wondering how it will perform on Linux.  Will it use the CPU to its full extent?  Or will it perform exactly the same as in Windows 10?   Do any of you guys have experience with this regard?


----------



## R-T-B (Jan 21, 2017)

grecinos said:


> Well... I decided to port My applet to a Java App.  I took the 1% inspiration, 99% perspiration approach.  After a significant amount of research, I was able to do it.  No mangling of the code, just well thought out adjustments.  So now, I can run the app from the command-line, whereas before, I was bound to the IDE.  Cool stuff.  In terms of speed improvement, initial tests show about the same.  Perhaps 1 or 2 fps better, but not much more.
> 
> Here are some other observations.  When I run my app, it uses about 20% CPU utilization.  When I spawn a second or third instance of the app, each instance also uses about 20% CPU utilization.  Since java is multi platform compatible, I'm wondering how it will perform on Linux.  Will it use the CPU to its full extent?  Or will it perform exactly the same as in Windows 10?   Do any of you guys have experience with this regard?



No OS is going to change your single-threaded workload limit.


----------



## Aquinus (Jan 21, 2017)

grecinos said:


> Do any of you guys have experience with this regard?


I'm a software engineer that has written multi-threaded code in production systems. I can tell you right now that nothing will use more of your CPU unless you modify it to take advantage of multiple cores and this isn't a task for the inexperienced if you want to do it right because doing it wrong can harm performance. AS @R-T-B said:


R-T-B said:


> No OS is going to change your single-threaded workload limit.


...and that it's on you as the dev to implement these changes if the time is worth the potential improvement. There are also trade-offs to altering a program to be multi-threaded. Usually the best way to achieve this is to decouple the "what" from the "when" by putting queues between each stage of your computation. That allows you to essentially parallize serial workloads. The problem here though is that while bandwidth will increase, latency will increase as well due to conveying values from one part of the application running on one thread to another part running on another. The other way is to take the most parallel part of your application and merely have more threads work on it which is basically the first suggestion plus some extra work to allow a single stage to have more than one worker.

All of these things incur overhead and require dev from a skilled dev with good understanding of computer science concepts to analyze if the cost of implementation is worth the speed up or if it would even result in any speed up at all. If you haven't profiled your application first, then you might want to start there. That would at least indentify which parts of your application are going slow relative to the rest of the application and multi-threading means nothing until you've done that. The world is a *very* different place when you start having to consider shared memory and multiple concurrent processes which is part of the reason why I use Clojure for most of my day-to-day dev needs that don't need to run somewhere where Clojure (the JVM; Java,) is available. Immutable data is a beautiful thing when it comes to concurrency.

tl;dr: Profile your app first before even considering multi-threading and multi-threading isn't for the faint of heart.


----------



## grecinos (Jan 23, 2017)

Thanks for the input Aquinus.  I think I'm going to leave my app the way it is.  If I implemented multi-threading, I might get a marginal improvement.  I don't think it's worth the effort.  From what I can tell, debugging an app of this sort would be a good recipe for a migraine .


----------



## Steevo (Jan 23, 2017)

Short answer? No.

Long answer, not without modifying application code for a multithreaded and slower in some scenarios approach.

If performance is really the issue, send if it can be written in something other than Java, as it's dying. 

Or get a faster single threaded CPU or overclock.


----------



## grecinos (Jul 7, 2018)

I had to resurrect this thread because I have taken upon myself to learn multi-threading in Java.  I created a double buffered animating app that uses a thread for drawing the images to a JFrame while another thread performs the logic (update calculations).  To make a long story short, I was able to increase the CPU usage from 18% to over 33%.  This a substantial improvement.  Something worth mentioning.  The app was designed with Array Lists of objects.  The Logic thread updates the objects while the primary thread draws them.  The inherent problem with this is that it generates Concurrent Execution errors.  So, I had to write clever coding to avoid this.  The best solution I was able to devise, was to make clones of the array lists before drawing them.  This way the primary thread has a copy of the data that won't be modified until the drawing phase has completed.  In the meantime, the logic thread calculates the next iteration.  It's not the most efficient technique, but it is certainly effective.  The other obstacle was to learn how to terminate the thread.  Without properly terminating the threads, your PC can lockup.  Essentially I made use of the "interrupt" and "join" methods inherited from the Thread class. I'm not 100% sure why this works, but I did create a destructor to confirm its functionality. 

So, in short, multi-threading, without a doubt can improve the performance of your Java App.  But you need to do the research as there are many pitfalls.  If anyone has questions or comments, feel free to respond.

Cheers,

grecinos


----------



## eidairaman1 (Jul 8, 2018)

You could always peruse oracle or whoever owns java and their forums. @FordGT90Concept, know anything about OOP?


----------



## FordGT90Concept (Jul 8, 2018)

This is still better done on GPU via HTML5:
https://www.html5canvastutorials.com/advanced/html5-canvas-animation-stage/

In Java, you should be creating a OpenGL render context and do your drawing in there (via GPU).  CPU should spend its time figuring out what to draw, not actually draw it.


If you're not looking for that major of a change, you could consider changing your array lists to arrays.  Array lists have a huge performance penalty when they have to expand their internal arrays.


----------



## OneMoar (Jul 8, 2018)

expecting efficient cpu usage from java is sorta like expecting a 300lb man not to use the handicap electric carts at the store 
its possible but extremely unlikely and generally a inconvenience for all


----------



## Aquinus (Jul 8, 2018)

FordGT90Concept said:


> If you're not looking for that major of a change, you could consider changing your array lists to arrays. Array lists have a huge performance penalty when they have to expand their internal arrays.


The correct way to do this in Java to maximize concurrency would be to use a stream IMHO.


OneMoar said:


> expecting efficient cpu usage from java is sorta like expecting a 300lb man not to use the handicap electric carts at the store
> its possible but extremely unlikely and generally a inconvenience for all


While your comment is colorful, it's not true or helpful. Java has performance similar to other JIT languages like C# and tends to be eclipsed only by languages like C and C++ which isn't a low bar. I'll concede that the JVM tends to consume a lot of memory but, that's more because if you give it more heap space, it will use it (kind of how the OS will do disk caching with free memory. It's there, so why not use it?) There are plenty of applications that can still run even if you reduce the max size of the heap as well. The JVM is also more than capable of using multiple hardware threads.


grecinos said:


> Essentially I made use of the "interrupt" and "join" methods inherited from the Thread class.


That's probably not the best way to go about it. The best way to handle mutable state with multiple threads is .wait and .notify. Join is appropriate if you're waiting for thread termination to indicate that processing is done but, spinning up threads is a relatively expensive task and you don't want to be spawning a thread every time you need to render a frame. I suggest using a stream because it decouples the consumer from the producer and doesn't require spinning up new threads often. It also opens the possibility of having multiple workers on either the producer or consumer side which could further improve performance depending on where the bottleneck is and if you can just throw more threads at the problem (OpenGL calls won't benefit from this.) Interrupt isn't a good option because you're basically telling the thread to halt execution, even if the task isn't yet complete. If you're not getting an InterruptedException, then the thread already has likely finished executing by the time .interrupt has been invoked otherwise something might not be completing every time.


----------



## R-T-B (Jul 8, 2018)

Aquinus said:


> While your comment is colorful, it's not true or helpful.



I know.  I go grocery shopping weekly without issues, and Java is pretty much as performant as any JIT while we're at it.


----------



## grecinos (Jul 9, 2018)

Thanks for the comments.

I'm not sure if I mentioned this in a prior reply, but I prefer Java because I find it to be sufficiently fast and cross platform compatible.  I installed Ubuntu Linux on my Windows 10 PC.  I'm able to run my app on it without modification or recompiling. Besides,  Java is a fun language to program in, IMO.

Aquinus:  

I'm using Interrupt and Join only when I restart the simulation (by clicking on a button).  I don't use it upon every iteration. During execution, no new threads are spawned.  

I would love to have access to the Nvidia GPU on my PC.  The problem I have is that my laptop has two graphics adapters.  It always defaults to the Intel GPU when running apps.  I wish I could use the discrete GPU without jumping through too many hoops, but I've yet to find a way to do that.


----------



## FordGT90Concept (Jul 9, 2018)

Ever tried C#?  It's like Java without the stupidity.  If you target .NET Framework 3.0 and conform the platform good practices, the same binary can run on Mono on Linux.  Another user here contributed OpenCL C# code that executes on as many GPUs as you have and balances the load amongst them: https://www.techpowerup.com/forums/...ultiple-gpus-and-cpu-at-the-same-time.232334/

HTML5 is the most cross-platform language there is.  If rendering, it uses the GPU that owns the screen.  Don't have experience with NVIDIA Optimus so I'm not sure how it handles browsers.  Not that it matters...if you're doing it on CPU now, even an integrated GPU should be significantly faster at the task.  CPUs really suck at rendering.  They're built for complex logic.  GPUs are built for doing simple tasks, over and over, across many processors.

HTML5 mostly builds off of JavaScript which should be familiar to you (not all that different from Java).


----------



## grecinos (Jul 9, 2018)

FordGT90Concept said:


> Ever tried C#? It's like Java without the stupidity. If you target .NET Framework 3.0 and conform the platform good practices, the same binary can run on Mono on Linux. Another user here contributed OpenCL C# code that executes on as many GPUs as you have and balances the load amongs them: https://www.techpowerup.com/forums/...ultiple-gpus-and-cpu-at-the-same-time.232334/



I haven't tired C#. I'll give it a whirl.  I'm downloading VS 2017 now.  How difficult is it to use DirectX utilizing a discrete GPU?  I'd like to start as simple as possible.  Say, animating circles that bounce off the edges of a "canvas".



FordGT90Concept said:


> HTML5 is the most cross-platform language there is. If rendering, it uses the GPU that owns the screen. Don't have experience with NVIDIA Optimus so I'm not sure how it handles browsers. Not that it matters...if you're doing it on CPU now, even an integrated GPU should be significantly faster at the task. CPUs really suck at rendering. They're built for complex logic. GPUs are built for doing simple tasks, over and over, across many processors.



So, HTML5 renders to the current GPU?  That was the problem I was having in Java.  It would render to the default GPU and not the more powerful, discrete GPU.



FordGT90Concept said:


> HTML5 mostly builds off of JavaScript which should be familiar to you (not all that different from Java).



I'm familiar with JavaScript.  I've never used it to create simulations, though.  Mainly for other web development purposes, such as validation, DOM, Jquery, AJAX, etc.  In terms of pure logic and arithmetic calculations, I don't believe it to be faster than Java.  Perhaps I'm mistaken?


----------



## FordGT90Concept (Jul 9, 2018)

grecinos said:


> I haven't tired C#. I'll give it a whirl.  I'm downloading VS 2017 now.  How difficult is it to use DirectX utilizing a discrete GPU?  I'd like to start as simple as possible.  Say, animating circles that bounce off the edges of a "canvas".


DirectDraw is for 2D, Direct3D is for 3D.  If you want to animate canvases and don't care about cross-platform, I'd recommend using Windows Presentation Foundation which is GPU accelerated but only available on Windows.  Here's basic WPF animations:
https://www.codeproject.com/articles/364529/animation-using-storyboards-in-wpf



grecinos said:


> So, HTML5 renders to the current GPU?  That was the problem I was having in Java.  It would render to the default GPU and not the more powerful, discrete GPU.


I imagine you'd have to go into Optimus settings and tell it to give the browser NVIDIA priority.



grecinos said:


> I'm familiar with JavaScript.  I've never used it to create simulations, though.  Mainly for other web development purposes, such as validation, DOM, Jquery, AJAX, etc.  In terms of pure logic and arithmetic calculations, I don't believe it to be faster than Java.  Perhaps I'm mistaken?


Java swing = CPU
HTML5 = GPU
Visual Studio WinForms = CPU
Windows Presentation Foundation = GPU

Example of bouncing box in HTML5 canvas:
http://jsfiddle.net/n2derqgw/4/


----------



## Aquinus (Jul 10, 2018)

FordGT90Concept said:


> It's like Java without the stupidity.


Now, now. I don't make fun of you for using .NET so I would keep statements like this to yourself. 


FordGT90Concept said:


> Java swing = CPU


Just don't use Swing. LWJGL gives you access to OpenGL bindings in Java.
https://www.lwjgl.org/


----------



## basco (Jul 10, 2018)

the only soft i know with java + multithreading should be 
https://hwbot.org/benchmark/hwbot_prime/


----------



## FordGT90Concept (Jul 10, 2018)

Aquinus said:


> Now, now. I don't make fun of you for using .NET so I would keep statements like this to yourself.


Two examples: 1) "Swing," what part of that sounds like UI to you? 2) lack of unsigned integers make any kind of binary data handling needlessly complicated (constantly have to convert  types).

Not talking about programmers, just Java in general is haphazard and poorly thought out.  The only thing going for it is it's cross-platform nature.



Aquinus said:


> Just don't use Swing. LWJGL gives you access to OpenGL bindings in Java.
> https://www.lwjgl.org/


There's the Java approach to GPU rendering.  If insisting on sticking with Java, probably the best path.


----------



## Vya Domus (Jul 10, 2018)

grecinos said:


> It would render to the default GPU and not the more powerful, discrete GPU.



Didn't read the entire thread , but what is it exactly that you are trying to render ? Java and graphics do not go well together if performance is your goal , no matter what you will do. If whatever it is that you are trying to do can be packed onto simple calculations over large vectors (likely to be the case if you are drawing pixels) OpenCL could be a solution and you wouldn't have to worry about any dedicated graphics API or cumbersome multithreading.



LFaWolf said:


> There is no single compiler directive or library that can instantly turn your app into multi-threaded, because that would be too easy



I know it's a year old comment but , believe it or not such a thing exists if you use C/C++ , it's OpenMP.


----------



## bug (Jul 10, 2018)

Easiest way to get the job done: use an Executor and start throwing jobs at it. https://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html

Also, get off Netbeans, IntelliJ Idea is vastly superior (and free for personal use). Its code analyzer is nothing short of miraculous.


----------



## grecinos (Jul 10, 2018)

Vya Domus said:


> Didn't read the entire thread , but what is it exactly that you are trying to render ?



I'm rendering polygons.  Nothing fancy.  The polygons interact with each other when they overlap.




Vya Domus said:


> OpenCL could be a solution and you wouldn't have to worry about any dedicated graphics API or cumbersome multithreading.



Thanks for the recommendation.  I'll look into it.




bug said:


> Easiest way to get the job done: use an Executor and start throwing jobs at it. https://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html
> 
> Also, get off Netbeans, IntelliJ Idea is vastly superior (and free for personal use). Its code analyzer is nothing short of miraculous.



I've been using Netbeans for a while now and have grown used to it.  I'll look into IntelliJ, sounds interesting.

I've since downloaded Visual C# and created a quick benchmark app consisting of a billion iterations, doing trigonometric computations.  Here's what the code looks like


```
...
for (int i=0; i<1000000000; i++)
{
          x = r * Math.cos(theta);
          y = r * Math.sin(theta);
          theta += Math.PI * 0.001;
          r += 0.1;
}
...
```

The time it took to execute is as follows:

C# (Fastest):  78715 ms

Java: (Fastest): 792166 ms


So (not too surprisingly), C# is effectively 10 times faster with the given code.  Now to create a simple benchmark with animation...


----------



## Aquinus (Jul 10, 2018)

FordGT90Concept said:


> 2) lack of unsigned integers make any kind of binary data handling needlessly complicated (constantly have to convert types).


Why are you converting between signed and unsigned in the first place? If you run out of values, use a long instead of an integer. If that's too small use a BigDecimal. Just because the human readable value is negative doesn't make it useless for the same operations and doesn't really change anything. It is "just bits", the negative just says a particular bit is a particular value. Bitwise operators and everything still work as expected on a signed variant versus an unsigned one. I see no difference between having 0-255 or -128-127 for a byte as they're representing the same thing. The only impact is how it's represented to a person but as far as the computer is concerned, a byte is a byte.


FordGT90Concept said:


> 1) "Swing," what part of that sounds like UI to you?


It doesn't and I have absolutely no intention of defending anything about Swing. 


grecinos said:


> C# (Fastest): 78715 ms
> 
> Java: (Fastest): 792166 ms


Trig functions in Java are a weird animal as they're intended to be accurate enough to do useful things with it, which means going a lot of decimal places out. There are faster implementations at the cost of accuracy. Depending on your use case, you might not need some as accurate as what is in java.lang.Math. Since you're only incrementing by 0.1, you could just make a lookup table for those values since a hash map lookup will be a lot faster than doing trig functions.
https://stackoverflow.com/questions/523531/fast-transcendent-trigonometric-functions-for-java


----------



## FordGT90Concept (Jul 10, 2018)

Aquinus said:


> Why are you converting between signed and unsigned in the first place?


Because files are almost exclusively handled as unsigned byte streams.  Java has to handle them as signed byte streams (who seriously ever uses a signed byte? so rare).  This especially gets complicated in things like animations where you have two unsigned bytes in a struct that can either be instructions or a signed short value.  Java has to do many steps when it should only be one or two.



Aquinus said:


> I see no difference between having 0-255 or -128-127 for a byte as they're representing the same thing.


Except that in the case of files, a negative value is usually impossible and because Java condemned that 8th bit to be useless, you've halved the upper bound of the value doubling the risk of overflow.


I'm trying to pin down a WPF physics sample that works but no luck so far...

Edit: I sort of broke Visual Studio so it's gonna be a while...


----------



## Aquinus (Jul 10, 2018)

FordGT90Concept said:


> Except that in the case of files, a negative value is usually impossible and because Java condemned that 8th bit to be useless, you've halved the upper bound of the value doubling the risk of overflow.


It still doesn't make a difference. It's twos compliment so the value that comes after 0111 1111 (127) is 1000 0000 (-128, or 128 unsigned,) and adding one to either the signed or unsigned representation will result in the same byte ordering of 1000 0001 (if you let it overflow or use a bitwise op instead of an arithmetic op.) It doesn't change any bitwise operation you would be using on it. It only changes how it's displayed to you. How you use them is still completely up to you. Just because it's different than what you're used to doesn't mean it's bad.


----------



## bug (Jul 10, 2018)

grecinos said:


> I'm rendering polygons.  Nothing fancy.  The polygons interact with each other when they overlap.
> 
> 
> 
> ...


Interesting test there. depending on what you do with those values, the compiler could decide it doesn't have to do anything, if the values are not used. Out of curiosity, what were the initial values?

One other thing, while I'm not familiar with C#, Java defaults to double which is in most cases overkill. I ran your code using float instead of double. It's almost 4x faster


----------



## FordGT90Concept (Jul 10, 2018)

Aquinus said:


> It still doesn't make a difference. It's twos compliment so the value that comes after 0111 1111 (127) is 1000 0000 (-128, or 128 unsigned,) and adding one to either the signed or unsigned representation will result in the same byte ordering of 1000 0001 (if you let it overflow or use a bitwise op instead of an arithmetic op.) It doesn't change any bitwise operation you would be using on it. It only changes how it's displayed to you. How you use them is still completely up to you. Just because it's different than what you're used to doesn't mean it's bad.


Just think about trying to read a GIF in Java.  You have a palette of 256 indexed colors and you have an unsigned byte array for each pixel that references a color in a specific frame.  In Java, you can't just read the bytes and pull the indexed color for each.  You have to read the byte, convert it to a signed short, and use that signed short to pull the indexed color from the array.  If you don't take that extra step, any index > 127 will give an out of bounds exception because negative indexes are invalid.  There's an extra step here Java forces on developers because it doesn't give access to the full range of primitives processors support.

That stupidity carries back to writing the updated GIF too.  All of those signed shorts Java forced you to convert to must be converted back to signed bytes for writing or else the file structure will become uncompliant with the standard.

C# not only lets you use all of the primitives, they're also classed (e.g. int.MaxValue, int.MinValue, and int.ToString() is a thing), they can be casted and converted between types, and you can change between signed and unsigned without changing the binary via (unchecked).  Literally everything anyone needs to do with binary data is baked in and practical.


----------



## Frick (Jul 10, 2018)

OneMoar said:


> expecting efficient cpu usage from java is sorta like expecting a 300lb man not to use the handicap electric carts at the store
> its possible but extremely unlikely and generally a inconvenience for all



Off topic but I used to be 330lb (now 240) and could walk just fine.


----------



## grecinos (Jul 10, 2018)

bug said:


> Interesting test there. depending on what you do with those values, the compiler could decide it doesn't have to do anything, if the values are not used. Out of curiosity, what were the initial values?
> 
> One other thing, while I'm not familiar with C#, Java defaults to double which is in most cases overkill. I ran your code using float instead of double. It's almost 4x faster



(Kindly FYI) This has nothing to do with my Polygon simulation.  But the entire (Java) "benchmark" code is as follows:


```
System.out.println("Calculations started...");
        long startTime = System.currentTimeMillis();
        double x, y;
        double r = 10;
        double theta = 0;

        for (int i=0; i<1000000000; i++)
        {
            x = r * Math.cos(theta);
            y = r * Math.sin(theta);
            theta += Math.PI * 0.001;
            r += 0.1;
        }
     
        long finishTime = System.currentTimeMillis();
        long elapsedTime = (finishTime-startTime);
     
        System.out.println("Calculations complete in: " + elapsedTime + " MilliSeconds");
```

When running it in a thread, I was gettings results as fast as 513055 ms.


----------



## bug (Jul 10, 2018)

grecinos said:


> (Kindly FYI) This has nothing to do with my Polygon simulation.  But the entire (Java) "benchmark" code is as follows:
> 
> 
> ```
> ...


Print x and y when you're done (for both Java and C#). If the compiler figures out you don't use those value, it may decide to not compute them at all. That way you can be 100% sure you're testing the math and not some compiler optimization.
And if you want to dig more into benchmarking, look up jmh  Java is way trickier to benchmark that it would seem.


----------



## Aquinus (Jul 10, 2018)

FordGT90Concept said:


> Just think about trying to read a GIF in Java. You have a palette of 256 indexed colors and you have an unsigned byte array for each pixel that references a color in a specific frame. In Java, you can't just read the bytes and pull the indexed color for each. You have to read the byte, convert it to a signed short, and use that signed short to pull the indexed color from the array. If you don't take that extra step, any index > 127 will give an out of bounds exception because negative indexes are invalid. There's an extra step here Java forces on developers because it doesn't give access to the full range of primitives processors support.


...or you could use a map of bytes to some object that describes everything needed for that color? There are other ways to do what you suggest without forcing the language to do it the way you want. There are right and wrong ways to do things in just about every language. If you need to look something up and you can't directly use an array, you just use a map.

As for the op, this same logic can apply but, with a slightly different problem.


grecinos said:


> (Kindly FYI) This has nothing to do with my Polygon simulation.  But the entire (Java) "benchmark" code is as follows:
> 
> 
> ```
> ...


The code here is going to run the same cos and sin methods with the same arguments 500000 times since you're repeating cos and sin after you pass 2π (360 degrees.) The op should be generating a lookup table for all values of cos(n) and sin(n) over a range of 0 to 2π, with a step of 0.001. One thousandth can be represented as an integer (1 = 1/1000) and that's your key. If you really felt so inclined, you could use an array.

```
public class Test {

  public static class TrigWrapper {
    public double sin;
    public double cos;

    public TrigWrapper(double s, double c) {
      this.sin = s;
      this.cos = c;
    }
  }

  public static TrigWrapper[] makeTrigMap() {
    double step = 0.001;
    double currentAngle;
    TrigWrapper[] output = new TrigWrapper[2000];
    for(int i = 0; i < 2000; i++) {
      currentAngle = i * (step * Math.PI);
      output[i] = new Test.TrigWrapper(Math.sin(currentAngle), Math.cos(currentAngle));
    }
    return output;
  }
}
```

With something like that, instead of having:

```
for (int i=0; i<1000000000; i++)
        {
            x = r * Math.cos(theta);
            y = r * Math.sin(theta);
            theta += Math.PI * 0.001;
            r += 0.1;
        }
```
You could have something like this:

```
Test.TrigWrapper[] lookupTable = Test.makeTrigMap();
        for (int i=0; i<1000000000; i++)
        {
            int n = i % 2000;
            x = r * lookupTable[n].cos;
            y = r * lookupTable[n].sin;
            r += 0.1;
        }
```
Edit: Don't mind my Java. I tend to write Clojure when I'm using the JVM. Logic might not be perfect but, you get the idea.

Edit 2: Making the lookup table (in Clojure which runs on the JVM,) takes very little time.

```
(defn make-lookup-table []
  (vec
    (for [i (range 2000)]
      (let [n (* (/ i 1000.0) Math/PI)]
        {:cos (Math/cos n)
         :sin (Math/sin n)}))))
```


```
(time (def table (make-lookup-table)))
> "Elapsed time: 2.466348 msecs"
> #'some-math.core/table
```

Edit 3: Lookup time, not too bad.

```
(time (get-in table [1373 :cos]))
> "Elapsed time: 0.079673 msecs"
> -0.3884807466313663
```


----------



## grecinos (Jul 11, 2018)

bug said:


> Print x and y when you're done (for both Java and C#). If the compiler figures out you don't use those value, it may decide to not compute them at all. That way you can be 100% sure you're testing the math and not some compiler optimization.
> And if you want to dig more into benchmarking, look up jmh  Java is way trickier to benchmark that it would seem.



I updated the code and got the same result. 

For the moment, what I'm doing, (drawing polygons as objects that interact), is more perceptual than functional at the moment.  It's doing what it's supposed to and appears as I envisioned it.  I'll see if I can port it to its C# equivalent for comparison purposes.


----------



## R-T-B (Jul 11, 2018)

Frick said:


> Off topic but I used to be 330lb (now 240) and could walk just fine.



I pointed it out for you above Frick.


----------



## FordGT90Concept (Jul 11, 2018)

I got this code working!
https://chriscavanagh.wordpress.com/2006/10/23/wpf-2d-physics/

Stuff attached.  I had to delete the compiled stuff in Source.zip so it was small enough to upload.  Open NewtonDynamics solution file first, compile that.  Then open the WPFPhysics solution and compile that.  Should have a working example then.  CodePlex didn't bother to save a unified solution file so had to rebuild them.



Aquinus said:


> ...or you could use a map of bytes to some object that describes everything needed for that color? There are other ways to do what you suggest without forcing the language to do it the way you want. There are right and wrong ways to do things in just about every language. If you need to look something up and you can't directly use an array, you just use a map.


A dictionary? Seriously?  The memory footprint balloons and performance plummets using a dictionary compared to an array for no reason other than Java's design stupidity.  No, just no.  Better off converting to and from short and sticking to an array.


----------



## Aquinus (Jul 11, 2018)

FordGT90Concept said:


> The memory footprint balloons and performance plummets using a dictionary compared to an array


Since those are obviously huge problems with a map with 256 values.


----------



## FordGT90Concept (Jul 11, 2018)

If it has millions of frames, it is.  That was just an example where Java is terrible.  It comes up frequently when dealing with files that weren't created by Java.  Can come up dealing with databases too.


----------



## Aquinus (Jul 11, 2018)

FordGT90Concept said:


> If it has millions of frames, it is.


A GIF with millions of frames, huh? If this was a video at 60fps, that would be 277 minutes worth of GIF. It also would be one huge file. 


FordGT90Concept said:


> It comes up frequently when dealing with files that weren't created by Java.


I read files that weren't created by Java all the time, however I can deal with the twos compliment bytes. In fact, most of the time I don't need to work with the byte directly but, when I do, I can deal with the twos complimented numbers Java *displays*.


----------



## Vya Domus (Jul 11, 2018)

I don't why this thread turned into an argument about Java. Java was probably one of the first languages designed for corporations by corporations and it shows , that's all there is to it. Not a whole lot of new software is built with Java from the ground up nowadays , sure there is a lot of maintenance to be done on the mountains of software that have been written in the early 2000s so it's usage somewhat skews the reality of things.

Java would literally by my last choice for 2D graphics or any kind of graphics , OP chose the wrong tool for the job , nothing will properly mend that or be worth the effort and hassle.


----------



## Aquinus (Jul 11, 2018)

Vya Domus said:


> Java would literally by my last choice for 2D graphics or any kind of graphics


I would argue that Node.js, Ruby, or PHP are less adequate than Java for rendered graphics but, I'm not disagreeing. Java wouldn't be my go-to for visualizations either, but that isn't to say it can't do it though and choosing to use the right tools inside the ecosystem helps as well. Fighting against a platform doesn't tend to help give you traction and Swing is probably the last thing you want to use.


----------



## Vya Domus (Jul 11, 2018)

Aquinus said:


> I would argue that Node.js, Ruby, or PHP are less adequate than Java for rendered graphics



Thing is Java falls into that grey area of languages that look like can be used for anything unlike those, who are widely recognized as being suitable for a specific set of tasks.


----------



## Aquinus (Jul 11, 2018)

Vya Domus said:


> Thing is Java falls into that grey area of languages that look like can be used for anything unlike those, who are widely recognized as being suitable for a specific set of tasks.


I know a lot of people who would argue that Node can be used for everything. People have opinions about these things but, at the end of the day, they're all Turing complete languages, each with their own strengths and weaknesses. I mean, at the end of the day if the only thing you care about is performance, you might as well just write it in C and call it a day. Java is very mature, it has a lot of well maintained libraries, and it runs everywhere. Those are things that are valuable to developers. I don't tend to write Java anymore, I usually write Clojure which, at the end of the day, compiles to Java byte code and runs on the JVM. It works for what I typically need and it gives me the ability to tap into any of the Java or Clojure libraries that have been written and put into Clojars or Maven Central.

For example, Google's cloud api library for Java is incredibly well maintained and the devs are very quick to address issues. If you're using something like GCP, that's worth its weight in gold to a business.


----------



## bug (Jul 11, 2018)

Aquinus said:


> I know a lot of people who would argue that Node can be used for everything. People have opinions about these things but, at the end of the day, they're all Turing complete languages, each with their own strengths and weaknesses. I mean, at the end of the day if the only thing you care about is performance, you might as well just write it in C and call it a day. Java is very mature, it has a lot of well maintained libraries, and it runs everywhere. Those are things that are valuable to developers. I don't tend to write Java anymore, I usually write Clojure which, at the end of the day, compiles to Java byte code and runs on the JVM. It works for what I typically need and it gives me the ability to tap into any of the Java or Clojure libraries that have been written and put into Clojars or Maven Central.
> 
> For example, Google's cloud api library for Java is incredibly well maintained and the devs are very quick to address issues. If you're using something like GCP, that's worth its weight in gold to a business.


I almost gave you thumbs up, but you had to say Clojure 
I'm more of a Scala guy 


Vya Domus said:


> Java would literally by my last choice for 2D graphics or any kind of graphics , OP chose the wrong tool for the job , nothing will properly mend that or be worth the effort and hassle.


What if you had to do _cross-platform_ graphics?
At the end of the day, it gets the job done. Ask Minecraft 
Plus, in the age of microservices, you can take advantage of Java's TTM and rewrite later if you need to squeeze more out of it. I mean, the OP seems to be doing just some prototyping atm.


----------



## erixx (Jul 11, 2018)

All i like about Java is that when I detect it, I reformat. (just kidding, but when I detect it, I go into hunter mode: What fucking application did bring this shit?))


----------



## bug (Jul 11, 2018)

erixx said:


> All i like about Java is that when I detect it, I reformat. (just kidding, but when I detect it, I go into hunter mode: What fucking application did bring this shit?))


It's probably not the best thing to have on your home PC, but it's on virtually every server out there. It powers useful stuff as Apache Kafka or Spark.


----------



## Aquinus (Jul 11, 2018)

bug said:


> I almost gave you thumbs up, but you had to say Clojure
> I'm more of a Scala guy


Ehhh. I've had enough of OO and the havoc it can wreak. Clojure(Script,) has the added benefit of being able to compile to JavaScript too which is nice for the browser (I personally really like Reagent.)



bug said:


> What if you had to do _cross-platform_ graphics?
> At the end of the day, it gets the job done. Ask Minecraft


It's a big reason why I suggested LWJGL if the OP is sticking with Java and Minecraft actually can run really well despite being on Java.


bug said:


> It's probably not the best thing to have on your home PC, but it's on virtually every server out there. It powers useful stuff as Apache Kafka or Spark.


Good enough for large scale applications running on servers, but not okay for workstations? Interesting.


----------



## FordGT90Concept (Jul 11, 2018)

Aquinus said:


> It's a big reason why I suggested LWJGL if the OP is sticking with Java and Minecraft actually can run really well despite being on Java.


It really doesn't.  It runs a lot better on the other platforms it has been ported to.  Even the UWP version.


----------



## bug (Jul 11, 2018)

Aquinus said:


> Ehhh. I've had enough of OO and the havoc it can wreak. Clojure(Script,) has the added benefit of being able to compile to JavaScript too which is nice for the browser (I personally really like Reagent.)


Yeah, functional solves many problems more elegantly. It's just that lisp syntax has never jived well with me, that's all.




Aquinus said:


> It's a big reason why I suggested LWJGL if the OP is sticking with Java and Minecraft actually can run really well despite being on Java.


Java's problem is not that it doesn't run well. In some benches it can match C for speed. Its problem is it needs a lot more memory than other solutions and it sometimes takes a while for JIT to kick in.



Aquinus said:


> Good enough for large scale applications running on servers, but not okay for workstations? Interesting.


Workstations wouldn't have a problem (other than having to juggle JREs), that's why I said _home PC_.


----------



## grecinos (Jul 13, 2018)

Vya Domus said:


> Java would literally by my last choice for 2D graphics or any kind of graphics , OP chose the wrong tool for the job , nothing will properly mend that or be worth the effort and hassle.



I took a course in Java when I was in college.  It was a great language to learn the concepts of OOP.  Most of what I'm doing is for proof of concept, and not so much  for professional consultation purposes.  There's plenty of resources available on the Web to answer just about any question you have about Java.  Java is also similar in syntax to other high level languages and I think that's a bonus.  I'm now dabbling in C# as per a recommendation on a prior post.  I'm finding the syntax very similar to Java.

P.S.  I'm no longer receiving email notifications for new posts in this thread...


----------



## FordGT90Concept (Jul 13, 2018)

C# exists because Sun sued Microsoft over using JVM in J++ (Microsoft adaptation of Java).  Microsoft went on to create the .NET Framework and J# (literally Java on .NET) and C# (C++ on .NET).  If you can hunt down a version of Visual Studio 2005, that was the last to have J#.  C# was so popular that they decided to axe support for J#.


----------



## grecinos (Jul 13, 2018)

FordGT90Concept said:


> C# exists because Sun sued Microsoft over using JVM in J++ (Microsoft adaptation of Java).



I remember J++ and I haven't used it in ages.  Interesting how things turned out.


----------

