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

  • SEARCH
  • Home
  • 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 8526981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:25:34+00:00 2026-06-11T08:25:34+00:00

I want to find geometric average of data and performance does matters. Which one

  • 0

I want to find geometric average of data and performance does matters.
Which one should I pick between

  1. Keep multiplication over single variable and take Nth-root at the end of calculation

    X = MUL(x[i])^(1/N)  
    

    Thus, O(N) x Multiplication Complexity + O(1) x Nth-root

  2. Use logarithm

    X = e ^ { 1/N * SUM(log(x[i])) }  
    

    Thus, O(N) x Logarithm Complexity + O(1) x Nth-division + O(1) Exponential

  3. Specialized algorithm for geometric average. Please tell me if there is.

  • 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-11T08:25:36+00:00Added an answer on June 11, 2026 at 8:25 am

    I thought I would try to benchmark this and get a comparison, here is my attempt.

    Comparing was difficult since the list of numbers needed to be large enough to make timing it reasonable, so N is large. In my test N = 50,000,000 elements.

    However, multiplying lots of numbers together which are greater than 1 overflows the double storing the product. But multiplying together numbers less than 1 gives a total product which is very small, and dividing by the number of elements gives zero.

    Just a couple more things: Make sure none of your elements are zero, and the Log approach doesn’t work for negative elements.

    (The multiply would work without overflow if C# had a BigDecimal class with an Nth root function.)

    Anyway, in my code each element is between 1 and 1.00001

    On the other hand, the log approach had no problems with overflows, or underflows.

    Here’s the code:

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting...");
            Console.WriteLine("");
    
            Stopwatch watch1 = new Stopwatch();
            Stopwatch watch2 = new Stopwatch();
    
            List<double> list = getList();
    
            double prod = 1;
    
            double mean1 = -1;
    
            for (int c = 0; c < 2; c++)
            {
                watch1.Reset();
    
                watch1.Start();
    
                prod = 1;
    
                foreach (double d in list)
                {
                    prod *= d;
                }
    
                mean1 = Math.Pow(prod, 1.0 / (double)list.Count);
    
                watch1.Stop();
    
            }
    
            double mean2 = -1;
    
            for (int c = 0; c < 2; c++)
            {
                watch2.Reset();
    
                watch2.Start();
    
                double sum = 0;
    
                foreach (double d in list)
                {
                    double logged = Math.Log(d, 2);
                    sum += logged;
                }
    
                sum *= 1.0 / (double)list.Count;
    
                mean2 = Math.Pow(2.0, sum);
    
                watch2.Stop();
    
            }
            Console.WriteLine("First way gave: " + mean1);
            Console.WriteLine("Other way gave: " + mean2);
    
            Console.WriteLine("First way took: " + watch1.ElapsedMilliseconds + " milliseconds.");
            Console.WriteLine("Other way took: " + watch2.ElapsedMilliseconds + " milliseconds.");
    
            Console.WriteLine("");
            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
        }
    
        private static List<double> getList()
        {
            List<double> result = new List<double>();
    
            Random rand = new Random();
    
            for (int i = 0; i < 50000000; i++)
            {
                result.Add( rand.NextDouble() / 100000.0 + 1);
            }
    
            return result;
        }
    }
    

    My computer output describes that both geometric means are the same, but that:

    Multiply  way took: 466 milliseconds
    Logarithm way took: 3245 milliseconds
    

    So, the multiply appears to be faster.

    But multiply is very problematic with overflow and underflow, so I would recommend the Log approach, unless you can guarantee the product won’t overflow and that the product won’t get too close to zero.

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

Sidebar

Related Questions

I want to find out the position of an element which is in iframe.
I want to find the Xelement attribute.value which children have a concrete attribute.value. string
I want to find a way to copy one file to multiple locations simultaneously
Given some horizontal line segments, I want to find a vertical line which intersects
i want find out the witch one is target-name space and witch one is
If I want find the differences between two directory trees, I usually just execute:
Preconditions: [numbers] [vip]111,222[vip] [standard]333[standard] [numbers] What I want: Find everything between [numbers] Problem: When
I have two tables one with cities and one with pois. I want find
I want find all Saturdays and Sundays in A given month. How can I
I want to find a struct in a vector, but I'm having some troubles.

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.