001package algs34;
002
003import stdlib.*;
004
005public class MyFBPerformanceTest {
006        static int MAX_PEOPLE = 50000;
007        static int NUM_SIZES = 6;
008        static MyFB.Person[] person = new MyFB.Person[MAX_PEOPLE];
009        public static void main(String[] args) {
010                StdRandom.setSeed (0);
011                StdOut.doNothing ();
012                MyFB.DEBUG = false;
013                for (int i=0; i<MAX_PEOPLE; i++) {
014                        person[i] = MyFB.makePerson (Integer.toString (i), 10);
015                }
016                addSpeedTest();
017                querySpeedTest();
018                lookupSpeedTest ();
019        }
020        public static void addSpeedTest() {
021                int NUM_RELATIONS = 25000;
022                for (int count=1; count<NUM_SIZES; count++) {
023                        NUM_RELATIONS += NUM_RELATIONS;
024                        int N = (int) (Math.sqrt (NUM_RELATIONS) * 3.0);
025                        MyFB fb = new MyFB ();
026                        Stopwatch sw1 = new Stopwatch ();
027                        for (int i=N-1; i>=0; i--)
028                                fb.addPerson (person[i]);
029                        for (int i=NUM_RELATIONS-1; i>=0; i--)
030                                fb.addFriendship (person[StdRandom.uniform (N)], person[StdRandom.uniform (N)]);
031                        double time1 = sw1.elapsedTime ();
032
033                        System.out.format ("%9d %9d: time=%f [add]\n", N, NUM_RELATIONS, time1);
034                }
035        }
036        public static void querySpeedTest() {
037                int NUM_RELATIONS = 25000;
038                for (int count=1; count<NUM_SIZES; count++) {
039                        NUM_RELATIONS += NUM_RELATIONS;
040                        int N = (int) (Math.sqrt (NUM_RELATIONS) * 3.0);
041                        MyFB fb = new MyFB ();
042                        for (int i=N-1; i>=0; i--)
043                                fb.addPerson (person[i]);
044                        for (int i=NUM_RELATIONS-1; i>=0; i--)
045                                fb.addFriendship (person[StdRandom.uniform (N)], person[StdRandom.uniform (N)]);
046                        Stopwatch sw1 = new Stopwatch ();
047                        for (int i=NUM_RELATIONS-1; i>=0; i--)
048                                fb.queryFriendship (person[StdRandom.uniform (N)], person[StdRandom.uniform (N)]);
049                        double time1 = sw1.elapsedTime ();
050
051                        System.out.format ("%9d %9d: time=%f [query]\n", N, NUM_RELATIONS, time1);
052                }
053        }
054
055        public static void lookupSpeedTest() {
056                int NUM_RELATIONS = 25000;
057                for (int count=1; count<NUM_SIZES; count++) {
058                        NUM_RELATIONS += NUM_RELATIONS;
059                        int N = (int) (Math.sqrt (NUM_RELATIONS) * 3.0);
060                        MyFB fb = new MyFB ();
061                        for (int i=N-1; i>=0; i--)
062                                fb.addPerson (person[i]);
063                        for (int i=NUM_RELATIONS-1; i>=0; i--)
064                                fb.addFriendship (person[StdRandom.uniform (N)], person[StdRandom.uniform (N)]);
065                        Stopwatch sw1 = new Stopwatch ();
066                        for (int i=NUM_RELATIONS-1; i>=0; i--)
067                                fb.lookupFriends (person[StdRandom.uniform (N)]);
068                        double time1 = sw1.elapsedTime ();
069
070                        System.out.format ("%9d %9d: time=%f [lookup]\n", N, NUM_RELATIONS, time1);
071                }
072        }
073}