KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > JScience


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2006 - JScience (http://jscience.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package org.jscience;
10
11 import java.math.BigInteger JavaDoc;
12 import java.util.Date JavaDoc;
13 import java.util.Random JavaDoc;
14
15 import javax.measure.quantities.*;
16 import javax.measure.units.*;
17
18 import org.jscience.economics.money.Currency;
19 import org.jscience.economics.money.Money;
20 import org.jscience.geography.coordinates.Altitude;
21 import org.jscience.geography.coordinates.CompoundCoordinates;
22 import org.jscience.geography.coordinates.LatLong;
23 import org.jscience.geography.coordinates.Time;
24 import org.jscience.geography.coordinates.UTM;
25 import org.jscience.geography.coordinates.XYZ;
26 import org.jscience.geography.coordinates.crs.CoordinatesConverter;
27 import org.jscience.mathematics.functions.Polynomial;
28 import org.jscience.mathematics.functions.Variable;
29 import org.jscience.mathematics.numbers.Complex;
30 import org.jscience.mathematics.numbers.Float64;
31 import org.jscience.mathematics.numbers.LargeInteger;
32 import org.jscience.mathematics.numbers.ModuloInteger;
33 import org.jscience.mathematics.numbers.Rational;
34 import org.jscience.mathematics.numbers.Real;
35 import org.jscience.mathematics.vectors.ComplexMatrix;
36 import org.jscience.mathematics.vectors.DenseMatrix;
37 import org.jscience.mathematics.vectors.DenseVector;
38 import org.jscience.mathematics.vectors.Float64Matrix;
39 import org.jscience.mathematics.vectors.Matrix;
40 import org.jscience.mathematics.vectors.Vector;
41 import org.jscience.physics.measures.Measure;
42 import org.jscience.physics.measures.MeasureFormat;
43 import org.jscience.physics.models.RelativisticModel;
44
45 import javolution.lang.MathLib;
46 import javolution.text.TextBuilder;
47 import javolution.context.LocalContext;
48 import javolution.context.PoolContext;
49 import static javax.measure.units.NonSI.*;
50 import static javax.measure.units.SI.*;
51 import static org.jscience.economics.money.Currency.*;
52
53 /**
54  * <p> This class represents the <b>J</b>Science library; it contains the
55  * {@link #main} method for versionning, self-tests, and performance
56  * analysis.</p>
57  *
58  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
59  * @version 3.0, February 13, 2006
60  */

61 public final class JScience {
62
63     /**
64      * Holds the version information.
65      */

66     public final static String JavaDoc VERSION = "@VERSION@";
67
68     /**
69      * Default constructor.
70      */

71     private JScience() {// Forbids derivation.
72
}
73
74     /**
75      * The library {@link #main} method. The archive <codejscience.jar</code>
76      * is auto-executable.
77      * <ul>
78      * <li><code>java [-cp javolution.jar] -jar jscience.jar version</code>
79      * to output version information.</li>
80      * <li><code>java [-cp javolution.jar] -jar jscience.jar test</code> to
81      * perform self-tests.</li>
82      * <li><code>java [-cp javolution.jar] -jar jscience.jar perf</code> for
83      * performance analysis.</li>
84      * </ul>
85      *
86      * @param args the option arguments.
87      * @throws Exception if a problem occurs.
88      */

89     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
90         System.out.println("JScience - Java(TM) Tools and Libraries for"
91                 + " the Advancement of Sciences");
92         System.out.println("Version " + VERSION + " (http://jscience.org)");
93         System.out.println("");
94         if (args.length > 0) {
95             if (args[0].equals("version")) {
96                 System.out.println("Version " + VERSION);
97                 return;
98             } else if (args[0].equals("test")) {
99                 testing();
100                 return;
101             } else if (args[0].equals("perf")) {
102                 benchmark();
103                 return;
104             }
105         }
106         System.out
107                 .println("Usage: java [-cp javolution.jar] -jar jscience.jar [arg]");
108         System.out.println("where arg is one of:");
109         System.out.println(" version (to show version information)");
110         System.out.println(" test (to perform self-tests)");
111         System.out.println(" perf (to run benchmark)");
112     }
113     
114     /**
115      * Performs simple tests.
116      *
117      * @throws Exception if a problem occurs.
118      */

119     private static void testing() throws Exception JavaDoc {
120         System.out.println("Testing...");
121         {
122             System.out.println("");
123             System.out.println("Exact Measurements");
124             Measure<Mass> m0 = Measure.valueOf(100, POUND);
125             Measure<Mass> m1 = m0.times(33).divide(2);
126             Measure<ElectricCurrent> m2 = Measure.valueOf("234 mA").to(
127                     MICRO(AMPERE));
128             System.out.println("m0 = " + m0);
129             System.out.println("m1 = " + m1);
130             System.out.println("m2 = " + m2);
131
132             System.out.println("");
133             System.out.println("Inexact Measurements");
134             Measure<Mass> m3 = Measure.valueOf(100.0, POUND);
135             Measure<Mass> m4 = m0.divide(3);
136             Measure<ElectricCurrent> m5 = Measure.valueOf("234 mA").to(AMPERE);
137             Measure<Temperature> t0 = Measure.valueOf(-7.3, 0.5, CELSIUS);
138             System.out.println("m3 = " + m3);
139             System.out.println("m4 = " + m4);
140             System.out.println("m5 = " + m5);
141             System.out.println("t0 = " + t0);
142
143             System.out.println("");
144             System.out.println("Interval measurements");
145             Measure<Volume> m6 = Measure.valueOf(20, 0.1, LITER);
146             Measure<Frequency> m7 = Measure.rangeOf(10, 11, KILO(HERTZ));
147             System.out.println("m6 = " + m6);
148             System.out.println("m7 = " + m7);
149
150             System.out.println("");
151             System.out.println("Measure.equals (identical) / Measure.approximates " +
152                     "(takes into account errors such as numeric errors)");
153             Measure<Frequency> m8 = Measure.valueOf(9000, HERTZ);
154             Measure<Frequency> m10 = m8.divide(3).times(3); // Still exact.
155
Measure<Frequency> m11 = m8.divide(7).times(7); // No more exact.
156
System.out.println("m8 = " + m8);
157             System.out.println("m10 = " + m10);
158             System.out.println("m11 = " + m11);
159             System.out.println("(m10 == m8) = " + m10.equals(m8));
160             System.out.println("(m10 ≅ m8) = " + m10.approximates(m8));
161             System.out.println("(m11 == m8) = " + m11.equals(m8));
162             System.out.println("(m11 ≅ m8) = " + m11.approximates(m8));
163
164             System.out.println("");
165             System.out.println("MeasureFormat - Plus/Minus Error (3 digits error)");
166             MeasureFormat.setInstance(MeasureFormat
167                     .getPlusMinusErrorInstance(3));
168             System.out.println("m3 = " + m3);
169             System.out.println("m4 = " + m4);
170             System.out.println("m5 = " + m5);
171
172             System.out.println("");
173             System.out.println("MeasureFormat - Bracket Error (2 digits error)");
174             MeasureFormat.setInstance(MeasureFormat.getBracketErrorInstance(2));
175             System.out.println("m3 = " + m3);
176             System.out.println("m4 = " + m4);
177             System.out.println("m5 = " + m5);
178
179             System.out.println("");
180             System.out.println("MeasureFormat - Exact Digits Only");
181             MeasureFormat.setInstance(MeasureFormat.getExactDigitsInstance());
182             System.out.println("m3 = " + m3);
183             System.out.println("m4 = " + m4);
184             System.out.println("m5 = " + m5);
185
186             System.out.println("");
187             System.out.println("Numeric Errors");
188             {
189                 Measure<Length> x = Measure.valueOf(1.0, METER);
190                 Measure<Velocity> v = Measure.valueOf(0.01, METER_PER_SECOND);
191                 Measure<Duration> t = Measure.valueOf(1.0, MICRO(SECOND));
192                 long ns = System.nanoTime();
193                 for (int i = 0; i < 10000000; i++) {
194                     x = x.plus(v.times(t));
195                 }
196                 ns = System.nanoTime() - ns;
197                 MeasureFormat.setInstance(MeasureFormat
198                         .getExactDigitsInstance());
199                 System.out.println(x
200                         + " ("
201                         + Measure.valueOf(ns, 0.5, NANO(SECOND)).to(
202                                 MILLI(SECOND)) + ")");
203             }
204             {
205                 double x = 1.0; // m
206
double v = 0.01; // m/s
207
double t = 1E-6; // s
208
for (int i = 0; i < 10000000; i++) {
209                     x += v * t; // Note: Most likely the compiler get v * t out of the loop.
210
}
211                 System.out.println(x);
212             }
213             MeasureFormat.setInstance(MeasureFormat
214                     .getPlusMinusErrorInstance(2));
215         }
216         {
217             System.out.println("");
218             System.out.println("Physical Models");
219             // Selects a relativistic model for dimension checking (typically at start-up).
220
RelativisticModel.select();
221
222             // Length and Duration can be added.
223
Measure<Length> x = Measure.valueOf(100, NonSI.INCH);
224             x = x.plus(Measure.valueOf("2.3 µs")).to(METER);
225             System.out.println(x);
226                
227             // Energy is compatible with mass (E=mc2)
228
Measure<Mass> m = Measure.valueOf("12 GeV").to(KILOGRAM);
229             System.out.println(m);
230         }
231
232         {
233             System.out.println("");
234             System.out.println("Money/Currencies");
235             ///////////////////////////////////////////////////////////////////////
236
// Calculates the cost of a car trip in Europe for an American tourist.
237
///////////////////////////////////////////////////////////////////////
238

239             // Use currency symbols instead of ISO-4217 codes.
240
UnitFormat.getStandardInstance().label(USD, "$"); // Use "$" symbol instead of currency code ("USD")
241
UnitFormat.getStandardInstance().label(EUR, "€"); // Use "€" symbol instead of currency code ("EUR")
242

243             // Sets exchange rates.
244
Currency.setReferenceCurrency(USD);
245             EUR.setExchangeRate(1.17); // 1.0 € = 1.17 $
246

247             // Calculates trip cost.
248
Measure<?> carMileage = Measure.valueOf(20, MILE
249                     .divide(GALLON_LIQUID_US)); // 20 mi/gal.
250
Measure<?> gazPrice = Measure.valueOf(1.2, EUR.divide(LITER)); // 1.2 €/L
251
Measure<Length> tripDistance = Measure.valueOf(400, KILO(SI.METER)); // 400 km
252
Measure<Money> tripCost = tripDistance.divide(carMileage).times(
253                     gazPrice).to(USD);
254             // Displays cost.
255
System.out.println("Trip cost = " + tripCost + " ("
256                     + tripCost.to(EUR) + ")");
257         }
258         {
259             System.out.println("");
260             System.out.println("Matrices/Vectors");
261
262             Measure<ElectricResistance> R1 = Measure.valueOf(100, 1, OHM); // 1% precision.
263
Measure<ElectricResistance> R2 = Measure.valueOf(300, 3, OHM); // 1% precision.
264
Measure<ElectricPotential> U0 = Measure.valueOf(28, 0.01, VOLT); // ±0.01 V fluctuation.
265

266             // Equations: U0 = U1 + U2 |1 1 0 | |U1| |U0|
267
// U1 = R1 * I => |-1 0 R1| * |U2| = |0 |
268
// U2 = R2 * I |0 -1 R2| |I | |0 |
269
//
270
// A * X = B
271
//
272
DenseMatrix<Measure<?>> A = DenseMatrix.valueOf(new Measure<?>[][] {
273                     { Measure.ONE, Measure.ONE, Measure.valueOf(0, OHM) },
274                     { Measure.ONE.opposite(), Measure.ZERO, R1 },
275                     { Measure.ZERO, Measure.ONE.opposite(), R2 } });
276             DenseVector<Measure<?>> B = DenseVector.valueOf(new Measure<?>[] { U0, Measure.valueOf(0,
277                     VOLT), Measure.valueOf(0, VOLT)});
278             Vector<Measure<?>> X = A.solve(B);
279             System.out.println(X);
280             System.out.println(X.get(2).to(MILLI(AMPERE)));
281         }
282         {
283             System.out.println("");
284             System.out.println("Polynomials");
285
286             // Defines two local variables (x, y).
287
Variable<Complex> varX = new Variable.Local<Complex>("x");
288             Variable<Complex> varY = new Variable.Local<Complex>("y");
289
290             // f(x) = 1 + 2x + ix²
291
Polynomial<Complex> x = Polynomial.valueOf(Complex.ONE, varX);
292             Polynomial<Complex> fx = x.pow(2).times(Complex.I).plus(
293                     x.times(Complex.valueOf(2, 0)).plus(Complex.ONE));
294             System.out.println(fx);
295             System.out.println(fx.pow(2));
296             System.out.println(fx.differentiate(varX));
297             System.out.println(fx.integrate(varY));
298             System.out.println(fx.compose(fx));
299
300             // Calculates expression.
301
varX.set(Complex.valueOf(2, 3));
302             System.out.println(fx.evaluate());
303         }
304
305         {
306             System.out.println("");
307             System.out.println("Coordinates Conversions");
308
309             // Simple Lat/Long to UTM conversion.
310
CoordinatesConverter<LatLong, UTM> latLongToUTM = LatLong.CRS
311                     .getConverterTo(UTM.CRS);
312             LatLong latLong = LatLong.valueOf(34.34, 23.56, DEGREE_ANGLE);
313             UTM utm = latLongToUTM.convert(latLong);
314             System.out.println(utm);
315
316             // Converts any projected coordinates to Latitude/Longitude.
317
//Coordinates<ProjectedCRS> coord2d = utm;
318
//ProjectedCRS<Coordinates> crs = coord2d.getCoordinateReferenceSystem();
319
//CoordinatesConverter<Coordinates, LatLong> cvtr = crs.getConverterTo(LatLong.CRS);
320
//latLong = cvtr.convert(coord2d);
321
//System.out.println(latLong);
322

323             // Compound coordinates.
324
Altitude alt = Altitude.valueOf(2000, FOOT);
325             CompoundCoordinates<LatLong, Altitude> latLongAlt = new CompoundCoordinates<LatLong, Altitude>(
326                     latLong, alt);
327             System.out.println(latLongAlt);
328
329             // Converts compound coordinates (3-D) to XYZ (GPS).
330
XYZ xyz = latLongAlt.getCoordinateReferenceSystem().getConverterTo(
331                     XYZ.CRS).convert(latLongAlt);
332             System.out.println(xyz);
333
334             // Even more compounding...
335
Time time = Time.valueOf(new Date JavaDoc());
336             CompoundCoordinates<CompoundCoordinates, Time> latLongAltTime = new CompoundCoordinates<CompoundCoordinates, Time>(
337                     latLongAlt, time);
338             System.out.println(latLongAltTime);
339         }
340
341         {
342             System.out.println("");
343             System.out.println("Numbers");
344
345             Real two = Real.valueOf(2); // 2.0000..00
346
Real.setExactMinimumPrecision(100); // Assumes 100 exact digits for exact numbers.
347
Real sqrt2 = two.sqrt();
348             System.out.println("sqrt(2) = " + sqrt2);
349             System.out.println("Precision = " + sqrt2.getPrecision()
350                     + " digits.");
351
352             LargeInteger dividend = LargeInteger.valueOf("3133861182986538201");
353             LargeInteger divisor = LargeInteger.valueOf("25147325102501733369");
354             Rational rational = Rational.valueOf(dividend, divisor);
355             System.out.println("rational = " + rational);
356
357             ModuloInteger m = ModuloInteger.valueOf("233424242346");
358             LocalContext.enter(); // Avoids impacting others threads.
359
try {
360                 ModuloInteger.setModulus(LargeInteger.valueOf("31225208137"));
361                 ModuloInteger inv = m.inverse();
362                 System.out.println("inverse modulo = " + inv);
363
364                 ModuloInteger one = inv.times(m);
365                 System.out.println("verification: one = " + one);
366
367             } finally {
368                 LocalContext.exit();
369             }
370
371         }
372     }
373     
374     /**
375      * Measures performance.
376      */

377     private static void benchmark() throws Exception JavaDoc {
378         System.out.println("Benchmark...");
379         //ConcurrentContext.setEnabled(false);
380
//PoolContext.setEnabled(false);
381

382         Object JavaDoc[] results = new Object JavaDoc[10000];
383
384         System.out.println("");
385         System.out.println("Numerics Operations");
386
387         System.out.print("Float64 add: ");
388         startTime();
389         for (int i = 0; i < 10000; i++) {
390             PoolContext.enter();
391             Float64 x = Float64.ONE;
392             for (int j = 0; j < results.length; j++) {
393                 results[j] = x.plus(x);
394             }
395             PoolContext.exit();
396         }
397         endTime(10000 * results.length);
398
399         System.out.print("Float64 multiply: ");
400         startTime();
401         for (int i = 0; i < 10000; i++) {
402             PoolContext.enter();
403             Float64 x = Float64.valueOf(1.0);
404             for (int j = 0; j < results.length; j++) {
405                 results[j] = x.times(x);
406             }
407             PoolContext.exit();
408         }
409         endTime(10000 * results.length);
410
411         System.out.print("Complex add: ");
412         startTime();
413         for (int i = 0; i < 10000; i++) {
414             PoolContext.enter();
415             Complex x = Complex.valueOf(1.0, 2.0);
416             for (int j = 0; j < results.length; j++) {
417                 results[j] = x.plus(x);
418             }
419             PoolContext.exit();
420         }
421         endTime(10000 * results.length);
422
423         System.out.print("Complex multiply: ");
424         startTime();
425         for (int i = 0; i < 10000; i++) {
426             PoolContext.enter();
427             Complex x = Complex.valueOf(1.0, 2.0);
428             for (int j = 0; j < results.length; j++) {
429                 results[j] = x.times(x);
430             }
431             PoolContext.exit();
432         }
433         endTime(10000 * results.length);
434
435         System.out.print("Measure (mass) add: ");
436         startTime();
437         for (int i = 0; i < 10000; i++) {
438             PoolContext.enter();
439             Measure<Mass> x = Measure.valueOf(1.0, SI.KILOGRAM);
440             for (int j = 0; j < results.length; j++) {
441                 results[j] = x.plus(x);
442             }
443             PoolContext.exit();
444         }
445         endTime(10000 * results.length);
446
447         System.out.print("Measure (mass) multiply: ");
448         startTime();
449         for (int i = 0; i < 10000; i++) {
450             PoolContext.enter();
451             Measure<Mass> x = Measure.valueOf(1.0, SI.KILOGRAM);
452             for (int j = 0; j < results.length; j++) {
453                 results[j] = x.times(x);
454             }
455             PoolContext.exit();
456         }
457         endTime(10000 * results.length);
458
459         System.out.println();
460         System.out.println("LargeInteger (PoolContext) versus BigInteger");
461         BigInteger JavaDoc big = BigInteger.probablePrime(1024, new Random JavaDoc());
462         byte[] bytes = big.toByteArray();
463         LargeInteger large = LargeInteger.valueOf(bytes, 0, bytes.length);
464
465         System.out.print("LargeInteger (1024 bits) addition: ");
466         startTime();
467         for (int i = 0; i < 1000; i++) {
468             PoolContext.enter();
469             for (int j = 0; j < results.length; j++) {
470                 results[j] = large.plus(large);
471             }
472             PoolContext.exit();
473         }
474         endTime(1000 * results.length);
475
476         System.out.print("LargeInteger (1024 bits) multiplication: ");
477         startTime();
478         for (int i = 0; i < 100; i++) {
479             PoolContext.enter();
480             for (int j = 0; j < results.length; j++) {
481                 results[j] = large.times(large);
482             }
483             PoolContext.exit();
484         }
485         endTime(100 * results.length);
486
487         System.out.print("BigInteger (1024 bits) addition: ");
488         startTime();
489         for (int i = 0; i < 1000; i++) {
490             for (int j = 0; j < results.length; j++) {
491                 results[j] = big.add(big);
492             }
493         }
494         endTime(1000 * results.length);
495
496         System.out.print("BigInteger (1024 bits) multiplication: ");
497         startTime();
498         for (int i = 0; i < 100; i++) {
499             for (int j = 0; j < results.length; j++) {
500                 results[j] = big.multiply(big);
501             }
502         }
503         endTime(100 * results.length);
504
505         System.out.println();
506         System.out.println("Matrix<Float64> and Matrix<Complex> versus "
507                 + "non-parameterized matrix (double)");
508         final int size = 500;
509
510         System.out.print("Non-parameterized matrix (double based)"
511                 + " 500x500 multiplication: ");
512         double[][] values = new double[size][size];
513         for (int i = 0; i < size; i++) {
514             for (int j = 0; j < size; j++) {
515                 values[i][j] = MathLib.random();
516             }
517         }
518         MatrixDouble PM = new MatrixDouble(values);
519         PM.times(PM); // Warming up.
520
startTime();
521         PM.times(PM);
522         endTime(1);
523
524         System.out.print("Matrix<Float64> 500x500 multiplication: ");
525         double[][] floats = new double[size][size];
526         for (int i = 0; i < size; i++) {
527             for (int j = 0; j < size; j++) {
528                 floats[i][j] = MathLib.random();
529             }
530         }
531         Matrix<Float64> FM = Float64Matrix.valueOf(floats);
532         FM.times(FM); // Warming up.
533
startTime();
534         FM.times(FM);
535         endTime(1);
536
537         System.out.print("Matrix<Complex> 500x500 multiplication: ");
538         Complex[][] complexes = new Complex[size][size];
539         for (int i = 0; i < size; i++) {
540             for (int j = 0; j < size; j++) {
541                 complexes[i][j] = Complex.valueOf(MathLib.random(), MathLib
542                         .random());
543             }
544         }
545         Matrix<Complex> CM = ComplexMatrix.valueOf(complexes);
546         CM.times(CM); // Warming up.
547
startTime();
548         CM.times(CM);
549         endTime(1);
550
551         System.out.print("Matrix<Measure> 500x500 multiplication: ");
552         Measure<?>[][] measures = new Measure<?>[size][size];
553         for (int i = 0; i < size; i++) {
554             for (int j = 0; j < size; j++) {
555                 measures[i][j] = Measure.valueOf(
556                         MathLib.random(Long.MIN_VALUE, Long.MAX_VALUE), Unit.ONE);
557             }
558         }
559         DenseMatrix<Measure<?>> MM = DenseMatrix.valueOf(measures);
560         startTime();
561         MM.times(MM);
562         endTime(1);
563
564         System.out.println();
565         System.out.println("More performance analysis in future versions...");
566     }
567
568     private static final class MatrixDouble {
569         double[][] o;
570
571         int m; // Nbr of rows.
572

573         int n; // Nbr of columns.
574

575         MatrixDouble(double[][] elements) {
576             o = elements;
577             m = elements.length;
578             n = elements[0].length;
579         }
580
581         MatrixDouble times(MatrixDouble that) {
582             if (that.m != this.n)
583                 throw new Error JavaDoc("Wrong dimensions");
584             MatrixDouble M = new MatrixDouble(new double[this.m][that.n]);
585             double[] thatColj = new double[n];
586             for (int j = 0; j < that.n; j++) {
587                for (int k = 0; k < n; k++) {
588                   thatColj[k] = that.o[k][j];
589                }
590                for (int i = 0; i < m; i++) {
591                   double[] thisRowi = o[i];
592                   double s = 0;
593                   for (int k = 0; k < n; k++) {
594                      s += thisRowi[k]*thatColj[k];
595                   }
596                   M.o[i][j] = s;
597                }
598             }
599             return M;
600         }
601     }
602
603     private static void startTime() {
604         _time = System.nanoTime();
605     }
606
607     /**
608      * Ends measuring time and display the execution time per iteration.
609      *
610      * @param iterations
611      * the number iterations performed since {@link #startTime}.
612      */

613     public static void endTime(int iterations) {
614         long nanoSeconds = System.nanoTime() - _time;
615         long picoDuration = nanoSeconds * 1000 / iterations;
616         long divisor;
617         String JavaDoc unit;
618         if (picoDuration > 1000 * 1000 * 1000 * 1000L) { // 1 s
619
unit = " s";
620             divisor = 1000 * 1000 * 1000 * 1000L;
621         } else if (picoDuration > 1000 * 1000 * 1000L) {
622             unit = " ms";
623             divisor = 1000 * 1000 * 1000L;
624         } else if (picoDuration > 1000 * 1000L) {
625             unit = " us";
626             divisor = 1000 * 1000L;
627         } else {
628             unit = " ns";
629             divisor = 1000L;
630         }
631         TextBuilder tb = TextBuilder.newInstance();
632         tb.append(picoDuration / divisor);
633         int fracDigits = 4 - tb.length(); // 4 digits precision.
634
tb.append(".");
635         for (int i = 0, j = 10; i < fracDigits; i++, j *= 10) {
636             tb.append((picoDuration * j / divisor) % 10);
637         }
638         System.out.println(tb.append(unit));
639     }
640
641     private static long _time;
642
643 }
Popular Tags