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