有人可以帮我解释为什么我的小数在输出的最后一行出错了吗?
这是我的输出:
Linkedlist的持续时间总和:3111480
LinkedList的平均值:31114
LinkedList的SD:9.65113011397E12Tree的时间总和:74628
树的平均值:1184
树的SD:5.486010029E9
树中的变量数:63HashSet的时间总和:33189
HashSet的平均值:526
HashSet的标准开发:1.085445182E9
HashSet中的变量数:63LinkedList(3111480),Tree(74628)和HashSet(33189)的最大时间总和为3111480
LinkedList(31114),Tree(1184)和HashSet(526)的最大平均时间为31114
LinkedList(9651130113970.000000),树(5486010029.00)和HashSet(1085445182.00)的最大标准偏差为9651130113970.00 错误的行
import java.util.*;
import java.util.TreeSet;
public class DataStructureTimingChris
{
public static void main(String[] args)
{
int MAXIMUM = 100;//initializing the maximum integer
int MINIMUM = 1;//initializing the minimum integer
int mean1 = 0;
int mean2 = 0;
int mean3 = 0;
//initializing the generation of random integers
Random randomGenerator = new Random();
//setting the range of integers from 1 to 1,000,000
int range = MAXIMUM - MINIMUM + 1;
//Array list for Hash Codes
ArrayList<Integer> randomNumbersHashCodes = new ArrayList<Integer>(5);
//Array list for Storage
ArrayList<Integer> randomNumbersStorage = new ArrayList<Integer>(5);
//for loop to generate 1,000,000 integers
for (int index = 1; index <= 100; ++index)
{
int randomInt = randomGenerator.nextInt(range) + MINIMUM;
randomNumbersStorage.add(randomInt);
randomNumbersHashCodes.add(String.valueOf(randomInt).hashCode());//storing randomly generated numbers in a ArrayList
}//end of for loop for random number generation and storage in an ArrayList
//array list to make string after converting int
ArrayList<String> randomNumbersStrg = new ArrayList<String>(randomNumbersHashCodes.size());
for (Integer myInt : randomNumbersHashCodes)
{
randomNumbersStrg.add(String.valueOf(myInt));
}
//initializing LinkedList
List<String> linkedList = new LinkedList<String>();
//adding generated hashcodes to Linked List
linkedList.addAll(randomNumbersStrg);
System.out.println();
List<Long> durations = new ArrayList<>(randomNumbersHashCodes.size());
for (int n : randomNumbersHashCodes)
{
long start = System.nanoTime();
randomNumbersHashCodes.contains(n); // true
long end = System.nanoTime();
durations.add(end - start);
}
//Initialize the tree
TreeSet<String> tree = new TreeSet<String>();
//for loop for tree to add randomNumbersStrg to tree
for (int x = 1; x <= 100; x++)
{
tree.addAll(randomNumbersStrg);
}
//for loop to get times of tree
List<Long> durations2 = new ArrayList<>(tree.size());
for (String m : tree)
{
long start2 = System.nanoTime();
tree.contains(m); // true
long end2 = System.nanoTime();
durations2.add(end2 - start2);
}
//to calculate mean and SD of Durations
int sum = 0;
double sd = 0;
for (int toCalcSum = 0; toCalcSum < durations.size(); toCalcSum++)
{
sum += durations.get(toCalcSum);
}
for (int toCalcSD = 0; toCalcSD < durations.size(); toCalcSD++)
{
sd += ((durations.get(toCalcSD) - sum) *
(durations.get(toCalcSD) - sum)) / (durations.size() - 1);
}
mean1 = sum/durations.size();
//to print out the results of Linked List
System.out.println("Sum of duration of Linkedlist: " + sum);
System.out.println("Mean of LinkedList: " + mean1);
System.out.println("SD of LinkedList: " + sd);
System.out.println();
//to calculate mean and SD of Durations2
int sum2 = 0;
double sd2 = 0;
for (int toCalcSum2 = 0; toCalcSum2 < durations2.size(); toCalcSum2++)
{
sum2 += durations2.get(toCalcSum2);
}
for (int toCalcSD2 = 0; toCalcSD2 < durations2.size(); toCalcSD2++)
{
sd2 += ((durations2.get(toCalcSD2) - sum2) *
(durations2.get(toCalcSD2) - sum2)) /
(durations2.size() - 1);
}
mean2 = sum2/durations2.size();
//to print out the results of Tree
System.out.println("Sum of time for Tree: " + sum2);
System.out.println("Mean of Tree: " + mean2);
System.out.println("SD of Tree: " + sd2);
System.out.println("# of variables in Tree: " + durations2.size());
System.out.println();
//to find Hashset
HashSet<String> hash = new HashSet<>();
for (int x = 1; x < randomNumbersStrg.size(); x++)
{
hash.addAll(randomNumbersStrg);
}
int sum3 = 0;
double sd3 = 0;
//for loop to get times of hash
List<Long> durationsHashSet = new ArrayList<>(hash.size());
for (String x : hash)
{
long start3 = System.nanoTime();
hash.contains(x); // true
long end3 = System.nanoTime();
durationsHashSet.add(end3 - start3);
}
for (int toCalcSum3 = 0; toCalcSum3 < durationsHashSet.size(); toCalcSum3++)
{
sum3 += durationsHashSet.get(toCalcSum3);
}
for (int toCalcSD3 = 0; toCalcSD3 < durationsHashSet.size(); toCalcSD3++)
{
sd3 += ((durationsHashSet.get(toCalcSD3) - sum3) *
(durationsHashSet.get(toCalcSD3) - sum3)) / (durationsHashSet.size() - 1);
}
mean3 = sum3 / durationsHashSet.size();
//to print out the results of Hashset
System.out.println("Sum of time for HashSet: " + sum3);
System.out.println("Mean of HashSet: " + mean3);
System.out.println("Std Dev of HashSet: " + sd3);
System.out.println("Number of variables in HashSet: " + durationsHashSet.size());
//to compare mean and standard Deviation
System.out.println();
System.out.printf("The maximum sum of time for LinkedList(%d),"
+ "Tree(%d) and HashSet(%d) is %d%n%n", sum, sum2, sum3,
maximum(sum, sum2, sum3));
System.out.printf("The maximum mean of time for LinkedList(%d),"
+ "Tree(%d) and HashSet(%d) is %d%n%n", mean1, mean2, mean3,
maximum(mean1, mean2, mean3));
System.out.printf("Maximum standard deviation for LinkedList(%f), "
+ "Tree(%.2f) and HashSet(%.2f) is %.2f%n%n", sd, sd2, sd3, maximum(sd, sd2, sd3));
}// end of main method
public static <T extends Comparable<T>> T maximum(T fromLinkedList, T fromTree, T fromHashSet)
{
T max = fromLinkedList; // assume fromLinkedList is initially the largest
if (fromTree.compareTo(max) > 0)
max = fromTree; // fromTree is the largest so far
if (fromHashSet.compareTo(max) > 0)
max = fromHashSet; // fromHashSet is the largest
return max; // returns the largest object
}
}//end of class
答案 0 :(得分:1)
在计算每个元素对方差的贡献时,您需要减去均值(而不是总和) - 然后将方差平方根得到标准差。
例如:
mean1 = sum/durations.size();
double var = 0;
for (int toCalcSD = 0; toCalcSD < durations.size(); toCalcSD++)
{
var += ((durations.get(toCalcSD) - mean1) *
(durations.get(toCalcSD) - mean1)) / (durations.size() - 1);
}
sd = Math.sqrt(var);