Παρασκευή 6 Οκτωβρίου 2023

CleanCodeBadPerformance Test

 

"Clean" Code, Horrible Performance

"Clean" Code, Horrible Performance - YouTube




loaded 10000000 Shapes

copied to arrays 3332483 3332550 3334967 ?=? 10000000 : 6664966 ,6665100 ,3334967

6664966 , 6665100 , 3334967

TotalArea1=4.741709582246646E8 in millis: 252

TotalArea2=4.741709582246208E8 in millis: 30 ok....!!!!!!

lesson learned!!!!!

BUILD SUCCESSFUL (total time: 4 seconds)



-------------------------------



/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package testcleancode;


import java.util.Random;

import java.util.Vector;


/**

 *

 * @author dimitrak

 * https://www.youtube.com/watch?v=tD5NrevFtbU

 */

public class TestCleanCode {

    static Random r=new Random();

    static Vector<Sx> v=new Vector<>();

    static double rv[];

    static double tv[];

    static double cv[];

    static int lr=0,lt=0,lc=0;

    static int ir=0,it=0,ic=0;

    /**

     * @param args the command line arguments"Clean" Code, Horrible Performance

     */

    public static void main(String[] args) {

        // TODO code application logic here

        prepareVect();

        test1();

        test2();

        

    }

    static void prepareVect(){

        int sxCount=10000000;

        int i=0;

        while(i<sxCount){

            int type=r.nextInt(3);

            if(type>0){

                if(type>1){

                    v.add(new Cir());

                    ++lc;

                }

                else{

                    v.add(new Tri());

                    ++lt;

                }

            }

            else{

                v.add(new Rec());

                ++lr;

            }

            ++i;

        }

        System.out.println("loaded "+sxCount+" Shapes");

        

        rv=new double[2*lr];

        tv=new double[2*lt];

        cv=new double[lc];

        i=0;

        while(i<sxCount){

            Sx s=v.get(i);

            if(s instanceof Rec){

                Rec rec =(Rec)s;

                rv[ir]=rec.w;

                ++ir;

                rv[ir]=rec.h;

                ++ir;

            }

            else if(s instanceof Tri){

                Tri tri =(Tri)s;

                tv[it]=tri.w;

                ++it;

                tv[it]=tri.h;

                ++it;

            }

            else{

                Cir cir =(Cir)s;

                cv[ic]=cir.r;

                ++ic;

            }

            ++i;

        }

        System.out.println("copied to arrays "+lr+" "+lt+" "+lc+" ?=? "+(lc+lr+lt)+" : "+rv.length+" ,"+tv.length+" ,"+cv.length);

        System.out.println(ir+" , "+it+" , "+ic);

    }

    static void test2(){

        double totArea2=0;

        

        int i=0;

        double w,h;

        long t2s=System.currentTimeMillis();


        i=0;

        while(i<rv.length){

            w=rv[i];

            ++i;

            h=rv[i];

            ++i;

            totArea2+=w*h;

            //System.out.println("rec("+w+","+h+")="+(w*h));

        }

        i=0;

        while(i<tv.length){

            w=tv[i];

            ++i;

            h=tv[i];

            ++i;

            totArea2+=(w*h)/2.0;

        }

        i=0;

        while(i<cv.length){

            w=cv[i];

            ++i;

            h=w;

            totArea2+=Math.PI*w*h;

        }

        long t2e=System.currentTimeMillis();

        long t2=t2e-t2s;

        

        

        System.out.println("TotalArea2="+totArea2+" in millis: "+t2);

        

        

    }

    static void test1(){

        

        

        int i=0;

        int l=v.size();

        double totArea1=0;

        long t1s=System.currentTimeMillis();

        while(i<l){

            totArea1+=v.get(i).area();

            ++i;

        }

        long t1e=System.currentTimeMillis();

        long t1=t1e-t1s;

        System.out.println("TotalArea1="+totArea1+" in millis: "+t1);

        


        

    }

    

}



abstract class Sx{

    

    public abstract double area();

}


class Rec extends Sx{

    public Rec(){

        w=10*TestCleanCode.r.nextDouble();

        h=10*TestCleanCode.r.nextDouble();

    }

    double w,h;

    public double area(){

        //System.out.println("Rec("+w+","+h+")="+(w*h));

        return w*h;

    }

}

class Tri extends Sx{

    public Tri(){

        w=10*TestCleanCode.r.nextDouble();

        h=10*TestCleanCode.r.nextDouble();

    }

    double w,h;

    public double area(){

        return (w*h)/2.0;

    }

}

class Cir extends Sx{

    double r;

    public Cir(){

        r=10*TestCleanCode.r.nextDouble();

    }

    public double area(){

        return Math.PI*r*r;

    }

}