1 /*
2  * Copyright (C) 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package benchmarks;
18 
19 /**
20  * What do various kinds of addition cost?
21  */
22 public class AdditionBenchmark {
timeAddConstantToLocalInt(int reps)23     public int timeAddConstantToLocalInt(int reps) {
24         int result = 0;
25         for (int i = 0; i < reps; ++i) {
26             result += 123;
27         }
28         return result;
29     }
timeAddTwoLocalInts(int reps)30     public int timeAddTwoLocalInts(int reps) {
31         int result = 0;
32         int constant = 123;
33         for (int i = 0; i < reps; ++i) {
34             result += constant;
35         }
36         return result;
37     }
timeAddConstantToLocalLong(int reps)38     public long timeAddConstantToLocalLong(int reps) {
39         long result = 0;
40         for (int i = 0; i < reps; ++i) {
41             result += 123L;
42         }
43         return result;
44     }
timeAddTwoLocalLongs(int reps)45     public long timeAddTwoLocalLongs(int reps) {
46         long result = 0;
47         long constant = 123L;
48         for (int i = 0; i < reps; ++i) {
49             result += constant;
50         }
51         return result;
52     }
timeAddConstantToLocalFloat(int reps)53     public float timeAddConstantToLocalFloat(int reps) {
54         float result = 0.0f;
55         for (int i = 0; i < reps; ++i) {
56             result += 123.0f;
57         }
58         return result;
59     }
timeAddTwoLocalFloats(int reps)60     public float timeAddTwoLocalFloats(int reps) {
61         float result = 0.0f;
62         float constant = 123.0f;
63         for (int i = 0; i < reps; ++i) {
64             result += constant;
65         }
66         return result;
67     }
timeAddConstantToLocalDouble(int reps)68     public double timeAddConstantToLocalDouble(int reps) {
69         double result = 0.0;
70         for (int i = 0; i < reps; ++i) {
71             result += 123.0;
72         }
73         return result;
74     }
timeAddTwoLocalDoubles(int reps)75     public double timeAddTwoLocalDoubles(int reps) {
76         double result = 0.0;
77         double constant = 123.0;
78         for (int i = 0; i < reps; ++i) {
79             result += constant;
80         }
81         return result;
82     }
83 }
84