SageTV Community  

Go Back   SageTV Community > SageTV Development and Customizations > SageTV Studio

Notices

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.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 05-06-2011, 05:10 PM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,805
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);
instead of this:

Code:
myMethod1(someLong.toString());
myMethod2(someLong.toString());
I'm assuming optimization has vastly improved and I do not need to do such things anymore. True?

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.
Reply With Quote
  #2  
Old 05-06-2011, 06:15 PM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
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());
Would the compiler be justified in "optimizing" this as follows?

Code:
String s= stack.pop();
myMethod1(s);
myMethod2(s);
Obviously not, because doing so would change the meaning of the program. But it's not obvious to the compiler that this case is any different than yours. They both involve identical calls to methods of objects. The fact that the method happens to be called "toString" is irrelevant. Unless the language has some formal mechanism for flagging specific methods as purely functional and free of side effects, the compiler cannot optimize out calls to those methods.
__________________
-- Greg
Reply With Quote
  #3  
Old 05-11-2011, 06:18 AM
broconne broconne is offline
Sage Aficionado
 
Join Date: Feb 2009
Location: Cary, NC
Posts: 306
Quote:
Originally Posted by tmiranda View Post
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);
instead of this:

Code:
myMethod1(someLong.toString());
myMethod2(someLong.toString());
I'm assuming optimization has vastly improved and I do not need to do such things anymore. True?

Where can I read about Java optimization so I can understand what code is expensive to run and what code is cheap?
It may be cliche but in my opinion it is more important to focus on readability rather than optimizing execution speed. If later you find your code is not performing as you would like, then it may be time for some targetted optimizations.

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.
Reply With Quote
  #4  
Old 05-13-2011, 05:50 AM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,805
Quote:
Originally Posted by broconne View Post
It may be cliche but in my opinion it is more important to focus on readability rather than optimizing execution speed.
This is my theory as well. I just wanted to make sure that by focusing on readability I wasn't tanking the performance.

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.
Reply With Quote
  #5  
Old 05-13-2011, 04:42 PM
david1234 david1234 is offline
Sage Aficionado
 
Join Date: Nov 2007
Location: Beaverton, OR
Posts: 311
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:
function(whatever.toString());
Lately, about the only place where I do a lot of code shortening is using the ? operator instead of an if-else block.
Quote:
BigDecimal smallerValue = (value1.compareTo(value2) <= 0) ? value1 : value2;
vs
Quote:
BigDecimal smallerValue;
if (value1compareTo(value2) <= )) {
smallerValue = value1;
} else {
smallerValue = value2
}
To me, it's just easier to read that the important part of the code is the setting of the smallerValue rather than the actual test of the other values

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:
if (somebool)
doStuff();
else
doOtherStuff();
movingOnToOtherThingsThatAreNotPartOfTheElse();

Last edited by david1234; 05-13-2011 at 04:44 PM.
Reply With Quote
  #6  
Old 05-13-2011, 07:23 PM
Fuzzy's Avatar
Fuzzy Fuzzy is offline
SageTVaholic
 
Join Date: Sep 2005
Location: Jurupa Valley, CA
Posts: 9,957
Quote:
Originally Posted by tmiranda View Post
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);
instead of this:

Code:
myMethod1(someLong.toString());
myMethod2(someLong.toString());
I'm assuming optimization has vastly improved and I do not need to do such things anymore. True?

Where can I read about Java optimization so I can understand what code is expensive to run and what code is cheap?
The compiler will not optimize this for you. You have to realize that in Object Oriented programming, someLong.tostring may not resolve the same every time. Of course, in this example, it would, but that assumption can't necessarily be made by the compiler. Remember, ToString is not some static transformation. It is, in itself, a completely separate function, that could, theoretically, return something different because of some other factor that may have changed in between those two method calls.

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
Reply With Quote
  #7  
Old 05-13-2011, 09:19 PM
Audacity Audacity is offline
Sage Advanced User
 
Join Date: Mar 2011
Location: Edmonton, AB, Canada
Posts: 91
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.
Reply With Quote
  #8  
Old 05-15-2011, 07:51 PM
sdsean's Avatar
sdsean sdsean is offline
Sage Expert
 
Join Date: Jul 2008
Posts: 537
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).
__________________
i7 3.33 GHz EE Quad Core+HT, 12GB DDR3 1333Mhz RAM, ATI HD6870 2GB VRM, 512GB Samsung SSD OS Drive, EVGA SLI x58 Mobo, Win7 Ultimatex64, Gyration MCE Remote, Hauppauge HD-DVR (x3), HD-HR, 2x H21-100, 1x H21-200 DirecTV STBs, 1 SIIG / CyberSerial 4 Port Serial PCI card, 1 PCIex1 Inland/NEC 5 port USB card
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT -6. The time now is 08:26 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Copyright 2003-2005 SageTV, LLC. All rights reserved.