test/examples/getter-setter-micro.js

Thu, 05 Sep 2019 18:59:46 +0800

author
aoqi
date
Thu, 05 Sep 2019 18:59:46 +0800
changeset 2493
2093c4a7abf0
parent 1074
29a4cd3d1f7a
permissions
-rw-r--r--

Merge

hannesw@1074 1 /*
hannesw@1074 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
hannesw@1074 3 *
hannesw@1074 4 * Redistribution and use in source and binary forms, with or without
hannesw@1074 5 * modification, are permitted provided that the following conditions
hannesw@1074 6 * are met:
hannesw@1074 7 *
hannesw@1074 8 * - Redistributions of source code must retain the above copyright
hannesw@1074 9 * notice, this list of conditions and the following disclaimer.
hannesw@1074 10 *
hannesw@1074 11 * - Redistributions in binary form must reproduce the above copyright
hannesw@1074 12 * notice, this list of conditions and the following disclaimer in the
hannesw@1074 13 * documentation and/or other materials provided with the distribution.
hannesw@1074 14 *
hannesw@1074 15 * - Neither the name of Oracle nor the names of its
hannesw@1074 16 * contributors may be used to endorse or promote products derived
hannesw@1074 17 * from this software without specific prior written permission.
hannesw@1074 18 *
hannesw@1074 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
hannesw@1074 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
hannesw@1074 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
hannesw@1074 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
hannesw@1074 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
hannesw@1074 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
hannesw@1074 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
hannesw@1074 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
hannesw@1074 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
hannesw@1074 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
hannesw@1074 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
hannesw@1074 30 */
hannesw@1074 31
hannesw@1074 32 /*
hannesw@1074 33 * A micro-benchmark for getters and setters with primitive values,
hannesw@1074 34 * alternating between ints and doubles. Introduction of primitive
hannesw@1074 35 * and optimistic user accessors in JDK-8062401 make this faster by
hannesw@1074 36 * 10x or more by allowing inlining and other optimizations to take place.
hannesw@1074 37 */
hannesw@1074 38
hannesw@1074 39 var x = {
hannesw@1074 40 get m() {
hannesw@1074 41 return this._m;
hannesw@1074 42 },
hannesw@1074 43 set m(v) {
hannesw@1074 44 this._m = v;
hannesw@1074 45 },
hannesw@1074 46 get n() {
hannesw@1074 47 return this._n;
hannesw@1074 48 },
hannesw@1074 49 set n(v) {
hannesw@1074 50 this._n = v;
hannesw@1074 51 }
hannesw@1074 52 };
hannesw@1074 53
hannesw@1074 54
hannesw@1074 55 function bench(v1, v2, result) {
hannesw@1074 56 var start = Date.now();
hannesw@1074 57 x.n = v1;
hannesw@1074 58 for (var i = 0; i < 1e8; i++) {
hannesw@1074 59 x.m = v2;
hannesw@1074 60 if (x.m + x.n !== result) {
hannesw@1074 61 throw "wrong result";
hannesw@1074 62 }
hannesw@1074 63 }
hannesw@1074 64 print("done in", Date.now() - start, "millis");
hannesw@1074 65 }
hannesw@1074 66
hannesw@1074 67 for (var i = 0; i < 10; i++) {
hannesw@1074 68 bench(i, 4, 4 + i);
hannesw@1074 69 }
hannesw@1074 70
hannesw@1074 71 for (var i = 0; i < 10; i++) {
hannesw@1074 72 bench(i, 4.5, 4.5 + i);
hannesw@1074 73 }
hannesw@1074 74
hannesw@1074 75 for (var i = 0; i < 10; i++) {
hannesw@1074 76 bench(i, 5, 5 + i);
hannesw@1074 77 }
hannesw@1074 78
hannesw@1074 79 for (var i = 0; i < 10; i++) {
hannesw@1074 80 bench(i, 5.5, 5.5 + i);
hannesw@1074 81 }

mercurial