diff options
author | Alexandre Rames <alexandre.rames@linaro.org> | 2016-08-01 08:48:29 +0000 |
---|---|---|
committer | Linaro Android Code Review <android-review@review.linaro.org> | 2016-08-01 08:48:29 +0000 |
commit | d615235841275895b5b58191c7b79245fb375603 (patch) | |
tree | 7e817fe91e8c5824e2a0b2fc23f971d31c7e713a | |
parent | d40702d55b7d59aff472f010f037d105f5abcfb9 (diff) | |
parent | 5ad5cfd32cb47b086d8b0f19b1925598048a2f1e (diff) | |
download | art-testing-d615235841275895b5b58191c7b79245fb375603.tar.gz |
Merge "Add benchmark for Exceptions code performance."
-rw-r--r-- | benchmarks/micro/Exceptions.java | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/benchmarks/micro/Exceptions.java b/benchmarks/micro/Exceptions.java new file mode 100644 index 0000000..457e42f --- /dev/null +++ b/benchmarks/micro/Exceptions.java @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2016 Linaro Limited. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/* + * Description: Exceptions code utilization. + * Main Focus: + * + */ + +package benchmarks.micro; + +public class Exceptions { + + private int[] smallArray = new int[1]; + private static final int ARITHM_DOUBLE_ITERS = 20000; + + /** + * Three following examples do the same arithmetics on double, but + * - arithmDouble: just does calculations + * - arirhmDoubleException: does calculations but has a try-catch which never fires. + * - arirhmDoubleExceptionFires: does calculations but has a try-catch which always fires. + */ + private int arithmDoubleExceptionFires(int n) { + double x = 1.0 * (double)n; + double y = 2.0 * (double)n; + double z = 3.0 * (double)n; + double expr = 0.0; + int a = 0; + + for (int i = 0; i < ARITHM_DOUBLE_ITERS; i++) { + expr += x + y + z * x + y * y + x * z; + try { + a = smallArray[1]; + } catch (Exception e) { + } + } + + return (int)expr + a + (int)x + (int)y + (int)z; + } + + private int arithmDoubleException(int n) { + double x = 1.0 * (double)n; + double y = 2.0 * (double)n; + double z = 3.0 * (double)n; + double expr = 0.0; + int a = 0; + + for (int i = 0; i < ARITHM_DOUBLE_ITERS; i++) { + expr += x + y + z * x + y * y + x * z; + try { + a = smallArray[0]; + } catch (Exception e) { + } + } + + return (int)expr + a + (int)x + (int)y + (int)z; + } + + private int arithmDouble(int n) { + double x = 1.0 * (double)n; + double y = 2.0 * (double)n; + double z = 3.0 * (double)n; + double expr = 0.0; + int a = 0; + + for (int i = 0; i < ARITHM_DOUBLE_ITERS; i++) { + expr += x + y + z * x + y * y + x * z; + a = smallArray[0]; + } + + return (int)expr + a + (int)x + (int)y + (int)z; + } + + public void timeArithmDoubleException(int iterations) { + for (int i = 0; i < iterations; i++) { + arithmDoubleException(i); + } + } + + public void timeArithmDoubleExceptionFires(int iterations) { + for (int i = 0; i < iterations; i++) { + arithmDoubleExceptionFires(i); + } + } + + public void timeArithmDouble(int iterations) { + for (int i = 0; i < iterations; i++) { + arithmDouble(i); + } + } + + public boolean verify() { + final int expected = 20600060; + return (arithmDouble(10) == expected) && + (arithmDoubleException(10) == expected) && + (arithmDoubleExceptionFires(10) == expected); + } + + private static final int TIME_FUNCTIONS_ITERS = 1000; + + public static void main(String[] args) { + int rc = 0; + Exceptions obj = new Exceptions(); + long before = System.currentTimeMillis(); + obj.timeArithmDoubleExceptionFires(10); + long after = System.currentTimeMillis(); + System.out.println("benchmarks/micro/Exceptions.arithmDoubleExceptionFires: " + + (after - before)); + + before = System.currentTimeMillis(); + obj.timeArithmDoubleException(TIME_FUNCTIONS_ITERS); + after = System.currentTimeMillis(); + System.out.println("benchmarks/micro/Exceptions.arithmDoubleException: " + (after - before)); + + before = System.currentTimeMillis(); + obj.timeArithmDouble(TIME_FUNCTIONS_ITERS); + after = System.currentTimeMillis(); + System.out.println("benchmarks/micro/Exceptions.arithmDouble: " + (after - before)); + + if (!obj.verify()) { + rc++; + } + + System.exit(rc); + } +} |