|
SageTV Studio Discussion related to the SageTV Studio application produced by SageTV. Questions, issues, problems, suggestions, etc. relating to the Studio software application should be posted here. |
|
Thread Tools | Search this Thread | Display Modes |
#1
|
||||
|
||||
How Smart is the Java Compiler?
I have a programming style question that I'd like some feedback on. In the old days I'd have to take great pain to make sure that I wrote code that was optimized. For example, if a derived variable was used multiple times I'd have to make sure it was calculated just once and then reused. Like this:
Code:
String longString = someLong.toString(); myMethod1(longString); myMethod2(longString); Code:
myMethod1(someLong.toString()); myMethod2(someLong.toString()); Where can I read about Java optimization so I can understand what code is expensive to run and what code is cheap?
__________________
Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders. |
#2
|
||||
|
||||
Optimization applies only to expressions that are transparent to the compiler so that it knows there will be no side effects from factoring out common subexpressions.
Consider: Code:
myMethod1(stack.pop()); myMethod2(stack.pop()); Code:
String s= stack.pop(); myMethod1(s); myMethod2(s);
__________________
-- Greg |
#3
|
|||
|
|||
Quote:
Optimizations items such as inlining, lock elision, etc, may change from release to release, and specially from JVM implementation to implementation.
__________________
Host: ESXi 6.5 w/ Intel Core i7 2.8GHZ 8GB Ram Guest: Ubuntu 16.04 with Sage v9 in Docker Tuners: 2 HDHR (OTA); Extenders: HD300 connected to a Samsung 56" DLP HDTV; HD300 connected to a Sharp 42" LCD Storage: OmniOS w/6 1TB Samsung Spinpoint in a RaidZ2 configuration in a 20 bay SATA hotswap case. |
#4
|
||||
|
||||
Quote:
When I look at other people's Java code it is written much more compactly than my code. Sometimes I look and see one line of code and I think if I had written that I would take 3 or 4 (or more) lines. Still being a Java noob I wanted to make sure I wasn't doing something stupid Thanks.
__________________
Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders. |
#5
|
||||
|
||||
If it were my code, I'd probably go with option 2. The time required to create the new object, is probably not that much less than just running toString a couple times.
Quote:
Quote:
Quote:
I think as long as you don't do this, just about anything goes. (people who don't use brackets {} on multi-line blocks should be shot) Quote:
Last edited by david1234; 05-13-2011 at 04:44 PM. |
#6
|
||||
|
||||
Quote:
That said, I agree with others here, that minor optimizations like this on modern processors are not exactly necessary. Proper readability will end up with more bug-free, and easier to maintain code, which, in the end, would be better.
__________________
Buy Fuzzy a beer! (Fuzzy likes beer) unRAID Server: i7-6700, 32GB RAM, Dual 128GB SSD cache and 13TB pool, with SageTVv9, openDCT, Logitech Media Server and Plex Media Server each in Dockers. Sources: HRHR Prime with Charter CableCard. HDHR-US for OTA. Primary Client: HD-300 through XBoxOne in Living Room, Samsung HLT-6189S Other Clients: Mi Box in Master Bedroom, HD-200 in kids room |
#7
|
|||
|
|||
When I'm initially writing code, I only pay much attention to optimization for inner loops.
Aside from that, I'd just go with doing whatever will make your code more maintainable, and then later on when the project is closer to completion (or release) run it through a profiler and optimize the hot spots. |
#8
|
||||
|
||||
Ok I'm gonna diverge a little bit here. . .
While I agree the readability is very important. . . I actually think optimizations should at least START at the begining. . . I agree that you will attack this over time, and you may not need to try an optimize as your coding. . . but. . . In cases like the one above. . .Its both readable, and obvious to only call toString() once. (Granted this is a very simple case). . .but it doesn't take away from readablility in my opinion. . . The biggest reason I say this for b/c especially when you are dealing with an environment that your code is loaded into, the API's you call will change over time. So. . .simple optimizations like this one, (where you minmize "." and mimize calls to functions unless necessary), from the start go a long way. Certainly, your code may still not be fast enough, and you have to keep going. . .but things like I just mentioned I wouldn't necessarily call unreadable, and they can help alot. . . (and also b/c Java, like many other languages, is run in a VM, and is so dynamic).
__________________
AMD Ryzen 9 3900XT 12 Core+HT, 64GB DDR5, GeForce 1060, MSI Prestige x570 Creation Mobo, SIIG 4 port Serial PCIe Card, Win10, 1TB M.2 SSD OS HDD, 1 URay HDMI Network Encoder, 3 HD-PVR, 4 DirecTV STB serial tuned |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Sage is too smart for me... | robhix | SageTV Software | 2 | 05-16-2009 03:16 PM |
Smart tuner selection? | MickBurke | SageTV Software | 4 | 10-17-2008 11:11 AM |
Is Sage really this smart? | Khidr | SageTV Software | 14 | 09-10-2008 12:12 PM |
Smart Stretch? | RBTConsultants | SageTV Software | 3 | 02-04-2007 04:19 PM |
How smart is Sage? | Mike Young | SageTV Beta Test Software | 14 | 11-01-2005 10:34 AM |