apply_to_call_bench.js revision 864:a9414c20b7e7
1/*
2 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * sanity check that apply to call specialization is faster than apply.
26 *
27 * @test
28  * @run
29 */
30
31var Class = {
32    create: function() {
33	return function() { //vararg
34	    this.initialize.apply(this, arguments);
35	}
36    }
37};
38
39Color = Class.create();
40Color.prototype = {
41    red: 0, green: 0, blue: 0,
42    initialize: function(r,g,b) {
43	this.red = r;
44	this.green = g;
45	this.blue = b;
46    }
47};
48
49var time1 = 0;
50var time2 = 0;
51
52function set_time1(t) {
53    time1 = t;
54}
55
56function set_time2(t) {
57    time2 = t;
58}
59
60function bench(x, set_time) {
61    var d = new Date;
62    var colors = new Array(16);
63    for (var i=0;i<1e8;i++) {
64	colors[i & 0xf] = new Color(1,2,3);
65    }
66    var t = new Date - d;
67    set_time(t);
68    return colors;
69}
70
71//warmup
72print("Running warmup");
73bench(17, set_time1);
74
75print("Running sharp run");
76bench(17, set_time1);
77
78print("Swapping out call");
79Function.prototype.call = function() {
80    throw "This should not happen, apply should be called instead";
81};
82
83print("Rerunning invalidated");
84bench(17, set_time2);
85
86print("All done!");
87
88if (time1 > time2) {
89    print("ERROR: time1 > time2 (" + time1 + " > " + time2 + ")");
90} else {
91    print("Times OK");
92}
93
94