Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8998285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:00:18+00:00 2026-06-16T00:00:18+00:00

I got curious about the way power calculation is done in Java and the

  • 0

I got curious about the way power calculation is done in Java and the performance of available methods. So I wrote a simple test to check on Math.pow(), * and ^ operations.

public static void main(String[] args) {

    int SIZE = 100000000;

    int[] arr1 = new int[SIZE];
    long st1, end1, st2, end2, st3, end3;

    st1 = System.currentTimeMillis();
    for (int i = 0; i < SIZE; i++) {
        arr1[i] = (int) Math.pow(i, 4);
    }
    end1 = System.currentTimeMillis();
    System.out.println("pow: " + (end1 - st1));

    arr1 = new int[SIZE];
    st2 = System.currentTimeMillis();
    for (int i = 0; i < SIZE; i++) {
        arr1[i] = i * i * i * i;
    }
    end2 = System.currentTimeMillis();
    System.out.println("mul: " + (end2 - st2));

    arr1 = new int[SIZE];
    st3 = System.currentTimeMillis();
    for (int i = 0; i < SIZE; i++) {
        arr1[i] = i^4;
    }
    end3 = System.currentTimeMillis();
    System.out.println("  ^: " + (end3 - st3));

    //to prevent optimizations form skipping the calculations
    for (int i = 0; i < SIZE; i++) {
        if (arr1[i] == 1){
            System.out.println(1);
        }
    }
    System.out.println("done");
}

and if the first two results were quite expected:

pow: 19253 19128 19205 19145 19185 19130 19162 19177 19191 19157 | 19173
mul: 91 86 91 85 98 90 90 105 87 95 | 92
  ^: 80 85 80 70 60 65 75 60 70 60  | 71

the third one is a bit confusing. How come ^ is always a bit faster than simple multiplication and which one should be used?

All the tests were run with JRE 1.7 in similar conditions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:00:19+00:00Added an answer on June 16, 2026 at 12:00 am

    The ^ operator is not performing exponentiation – it’s a bitwise “exclusive OR” (aka “xor”).

    Using integer math for 100000000 raised to the fourth power will give incorrect results – a 32-bit integer cannot store numbers that large.

    Math.pow() will use floating point arithmetic. The answers may not be 100% accurate due to precision issues, but should be capable of representing the required range of results.

    To get 100% accurate values for numbers that large, you should use the BigInteger class. However it will not be particularly fast. This is a trade off you have to make when considering accuracy vs performance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basic question here, just got curious about coding a simple formula out, and wanted
Came by a curious case today, that got me thinking about how the object
I was curious about the performance of insert-sort using C and python but the
I'm making some pretty string-manipulation-intensive code in C#.NET and got curious about some Joel
I got curious and have been reading about GUI development using Python for the
I know that NSSet does not preserve order and I got curious about how
I've got a curious problem. After installing java jdk 6u25 I tested the installation
I'm currently studying Spring and suddenly, I've got curious about DI And my question
I saw some code on an unrelated question but it got me curious as
got a simple web site, in html. I do a simple function to move

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.