1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
// (jEdit options) :folding=explicit:collapseFolds=1:
//{{{ Package, imports
//package quickies;
//import java.awt.*;
//import java.awt.event.*;
import java.io.*;
//import java.net.*;
//import java.text.*;
import java.util.*;
//import java.util.regex.*;
//import javax.swing.*;
//}}}
/**
* <code>FloatVsDouble</code> tests computational performance on
* floats versus that on doubles.
*
* For 10^6 multiply-add-assign operations,
* Loop for doubles was 286 ms (283 - 293)
* Loop for floats was 277 ms (273 - 283)
*
* For 10^7 loops, the performance was indistinguishable.
*
* <p>Copyright (C) 2003 by Ian W. Davis. All rights reserved.
* <br>Begun on Fri Feb 7 13:38:20 EST 2003
*/
public class FloatVsDouble //extends ... implements ...
{
//{{{ Constants
//}}}
//{{{ Variable definitions
//##################################################################################################
//}}}
//{{{ Constructor(s)
//##################################################################################################
/**
* Constructor
*/
public FloatVsDouble()
{
}
//}}}
//{{{ empty_code_segment
//##################################################################################################
//}}}
//{{{ Main() and main()
//##################################################################################################
/**
* Main() function for running as an application
*/
public void Main()
{
double a = Math.PI;
double b = Math.E;
double c = 0;
/*
float a = (float)Math.PI;
float b = (float)Math.E;
float c = 0;
*/
long time = System.currentTimeMillis();
for(int i = 0; i < 10000000; i++)
{
c = c*b + a;
}
time = System.currentTimeMillis() - time;
System.out.println("Finished in "+time+" ms.");
}
public static void main(String[] args)
{
FloatVsDouble mainprog = new FloatVsDouble();
try
{
mainprog.parseArguments(args);
mainprog.Main();
}
catch(IllegalArgumentException ex)
{
ex.printStackTrace();
System.err.println();
mainprog.showHelp(true);
System.err.println();
System.err.println("*** Error parsing arguments: "+ex.getMessage());
System.exit(1);
}
}
//}}}
//{{{ Argument parsing, etc.
//##################################################################################################
/**
* Parse the command-line options for this program, separating out parameters from flags.
* @param args the command-line options, as received by main()
* @throws IllegalArgumentException if any argument is unrecognized, ambiguous, missing
* a required parameter, has a malformed parameter, or is otherwise unacceptable.
*/
void parseArguments(String[] args)
{
/* Load properties from file * /
// System properties from JAR file -- must be present
Props props = new Props();
InputStream is = getClass().getResourceAsStream("/rc/FloatVsDouble.props");
try { props.load(is); } catch(IOException ex) {ex.printStackTrace();}
// User properties from home dir -- may be present
File userfile = new File(System.getProperty("user.home")+System.getProperty("file.separator")+".FloatVsDouble.props");
// Load user properties from file, if found
if(userfile.exists())
{
try {
is = new FileInputStream(userfile);
props.load(is);
System.err.println("Found user properties in '"+userfile+"'");
} catch(IOException ex) {ex.printStackTrace();}
}
// User properties from current dir -- may be present
File localfile = new File(System.getProperty("user.dir")+System.getProperty("file.separator")+"FloatVsDouble.props");
// Load user properties from file, if found
if(localfile.exists())
{
try {
is = new FileInputStream(localfile);
props.load(is);
System.err.println("Found user properties in '"+localfile+"'");
} catch(IOException ex) {ex.printStackTrace();}
}
/* Load properties from file */
// Parse argument array into a linked list,
// converting "-flag=param" to "-flag" "param"
LinkedList arglist = new LinkedList();
int iEquals;
boolean interpFlags = true;
for(int i = 0; i < args.length; i++)
{
if(args[i].equals("--"))
{
interpFlags = false;
arglist.add(args[i]);
}
else if(args[i].startsWith("-") && interpFlags)
{
iEquals = args[i].indexOf('=');
if(iEquals != -1)
{
arglist.add( args[i].substring(0,iEquals) );
arglist.add( args[i].substring(iEquals+1) );
}
else arglist.add(args[i]);
}
else arglist.add(args[i]);
}
ListIterator iter = arglist.listIterator();
try
{
//interpretArguments(iter, props);
interpretArguments(iter);
}
catch(NoSuchElementException ex)
{
throw new IllegalArgumentException("'"+iter.previous()+"' expected an argument");
}
}
// Get an integer from the command line argument list
int getInt(Iterator iter)
{
String param = iter.next().toString();
try { return Integer.parseInt(param); }
catch(NumberFormatException ex)
{ throw new IllegalArgumentException("'"+param+"' is not an integer"); }
}
// Get a double from the command line argument list
double getDouble(Iterator iter)
{
String param = iter.next().toString();
try { return Double.parseDouble(param); }
catch(NumberFormatException ex)
{ throw new IllegalArgumentException("'"+param+"' is not a real number"); }
}
// Get a reader for a file name, dash (-) for stdin
static private LineNumberReader SYSTEM_IN_READER = null;
LineNumberReader getReader(String filename)
{
LineNumberReader r = null;
if(filename.equals("-"))
{
if(SYSTEM_IN_READER == null) SYSTEM_IN_READER = new LineNumberReader(new InputStreamReader(System.in));
r = SYSTEM_IN_READER;
}
else
{
try { r = new LineNumberReader(new FileReader(filename)); }
catch(IOException ex)
{ throw new IllegalArgumentException("'"+filename+"' is not a readable file or stream."); }
}
return r;
}
// Get a writer for a file name, dash (-) for stdout
static private PrintWriter SYSTEM_OUT_WRITER = null;
PrintWriter getWriter(String filename)
{
PrintWriter w = null;
if(filename.equals("-"))
{
if(SYSTEM_OUT_WRITER == null) SYSTEM_OUT_WRITER = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
w = SYSTEM_OUT_WRITER;
}
else
{
try { w = new PrintWriter(new BufferedWriter(new FileWriter(filename))); }
catch(IOException ex)
{ throw new IllegalArgumentException("'"+filename+"' is not a writable file or stream."); }
}
return w;
}
// Display help information
void showHelp(boolean showAll)
{
if(showAll)
{
InputStream is = getClass().getResourceAsStream("/rc/FloatVsDouble.help");
if(is == null) System.err.println("\n*** Unable to locate help information in '/rc/FloatVsDouble.help' ***\n");
else
{
try { streamcopy(is, System.out); }
catch(IOException ex) { ex.printStackTrace(); }
}
}
//System.err.println("quickies.FloatVsDouble, version "+Version.VERSION+" (build "+Version.BUILD+")");
System.err.println("Copyright (C) 2003 by Ian W. Davis. All rights reserved.");
}
// Copies src to dst until we hit EOF
void streamcopy(InputStream src, OutputStream dst) throws IOException
{
byte[] buffer = new byte[2048];
int len;
while((len = src.read(buffer)) != -1) dst.write(buffer, 0, len);
}
//}}}
//{{{ Argument interpretation
//##################################################################################################
//void interpretArguments(Iterator iter, Props props)
void interpretArguments(Iterator iter)
{
String arg;
boolean interpFlags = true;
ArrayList files = new ArrayList();
while(iter.hasNext())
{
arg = iter.next().toString();
if(!arg.startsWith("-") || !interpFlags || arg.equals("-"))
{
files.add(arg);
}
else if (arg.equals("--")) interpFlags = false;
else if(arg.equals("-help") || arg.equals("-h")) {
showHelp(true);
System.exit(0);
} else if(arg.equals("-dummy_option")) {
// handle option here
} else {
throw new IllegalArgumentException("'"+arg+"' is not recognized as a valid argument.");
}
}
// Post-processing, adding things to props, etc.
}
//}}}
}//class
|