Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (526)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  short parameters  (Read 2515 times)
0 Members and 1 Guest are viewing this topic.
Offline donmc

Junior Devvie


Projects: 1



« Posted 2008-07-19 16:01:21 »

public void foo(short bar)
{
}

foo(123); // compile error because the literal is converted to an int
foo((short)123); // works but icky
foo(123s); // I would like to do something like this...

Are there any tricks to get around having to manually typecast the literal using (short)?

I would like to save the 2 bytes but I would also like to avoid typing 7 extra characters each time.
Offline IntuitSea!

Senior Newbie





« Reply #1 - Posted 2008-07-19 16:30:52 »

First, if you do any lengthy calculations, make sure to use an int.  If you use shorts, it will be slow because Java will convert all of them to ints.  I couldn't find a solution like you are wanting probably because Java wants you to type cast to warn people about what I just said.  Having 2s might make people think that "short s = 2s + 4s + 5s" is just as fast as "short s = (short)(2 + 4 + 5)" when it's not.  Hopefully some one else will come along and give a better solution, but here are some solutions I can offer:

1  
2  
3  
public void foo(int i) {
  short s = (short)i;
}


1  
2  
3  
4  
5  
6  
public void foo(short s) {
}

public void foo(int i) {
  foo((short)i);
}

-Amazing-
Offline Abuse

JGO Knight


Medals: 14


falling into the abyss of reality


« Reply #2 - Posted 2008-09-13 12:04:50 »

Avoid embedding literals in your source?  persecutioncomplex

1  
2  
final short someMeaningfulNameForTheLiteral = 123;
foo(someMeaningfulNameForTheLiteral);

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #3 - Posted 2008-09-13 15:40:55 »

I would like to save the 2 bytes but I would also like to avoid typing 7 extra characters each time.

You do realise that using a short instead of an int isn't really going to save you anything, right?

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2008-09-13 15:49:04 »

unless you're working with huge int arrays, which can be turned into short arrays.

but I doubt he's running into a memory bandwidth bottleneck here

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #5 - Posted 2008-09-13 16:06:46 »

unless you're working with huge int arrays, which can be turned into short arrays.

but I doubt he's running into a memory bandwidth bottleneck here
True, but the original code only shows a method parameter. It's pretty rare to ever need a short instead of an int as a method param.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline xinaesthetic

Senior Devvie


Medals: 1



« Reply #6 - Posted 2008-09-13 22:18:56 »

First, if you do any lengthy calculations, make sure to use an int.  If you use shorts, it will be slow because Java will convert all of them to ints.
Indeed, a colleague (actually, my dad - nepotism ftw Wink) recently did some benchmarks of some intensive number-crunching in c# to see what difference various basic tweaks would make.  It was basically synthesising a load of sin waves at audio rate via totally straightforward calls to Math.sin() IIRC.  Doing processing with floats rather than doubles had a significant impact; significantly slower with floats!  I can't remember how big the difference was, and I wouldn't rate the precision of the benchmark anyway, but it was enough to be quite clearly significant.

edit: it's just occurred to me that if it was indeed calling Math.sin(), that's fairly obviously the cause of the slowness given that it uses doubles, requiring lots of casts in the float case. Now I feel like I've brought shame on my whole family... shucks.  I suppose the point that casts are costly, which was already made by Intuit, is the real point.
Offline xinaesthetic

Senior Devvie


Medals: 1



« Reply #7 - Posted 2008-09-15 11:49:47 »

For the record, the test used lookup tables, not Math.sin().  Not sure why I suddenly thought otherwise.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (72 views)
2014-11-26 15:22:04

toopeicgaming1999 (62 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (27 views)
2014-11-24 19:59:16

trollwarrior1 (40 views)
2014-11-22 12:13:56

xFryIx (78 views)
2014-11-13 12:34:49

digdugdiggy (56 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!