# Finding The Geometric Mean In Python

Geometric means are a quick and easy way to benchmark system/interpreter performance. The below function is an example of calculating geometric averages in Python:
{{{ lang=python
def geometric_mean(nums):
”’
Return the geometric average of nums
@param list nums List of nums to avg
@return float Geometric avg of nums
”’
return (reduce(lambda x, y: x*y, nums))**(1.0/len(nums))
}}}
I use the below script when I am comparing different Python interpreters, and I also use it to determine when to offload from specific servers due to low performance.
{{{ lang=python
#!/usr/bin/env python
from random import randint
from timeit import timeit

def geometric_mean(nums):
”’
Return the geometric average of nums
@param list nums List of nums to avg
@return float Geometric avg of nums
”’
return (reduce(lambda x, y: x*y, nums))**(1.0/len(nums))

def random_means(loop_cnt=1):
”’
Loop geometric_mean multiple times, use random seeds
@param int loop_cnt Amount of times to loop
”’
seed_nums = [0,0,0,0,0] #< Will be randomly filled in below loop for i in xrange(0, loop_cnt): for j in xrange(0, 5): seed_nums[j] = randint(0, 1000) geometric_mean(seed_nums) # Loop 10,000 iterations of random_means(), print the amount of time it takes print timeit(random_means, number=10000) }}}

Posted

in

by

Tags: