Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


Deep Copy, Shallow Copy¿¡ °üÇÑ Perfomance Test ¹× °í·Á»çÇ×
 
°´Ã¼¸¦ º¹»çÇÏ´Â ¹æ¹ý¿¡ À־ ¼º´É¿¡ °ü·ÃµÈ À̽´¸¦ Å×½ºÆ® Çغ»´Ù. Æ®¸®, ±×·¡ÇÁµîÀÇ ÀڷᱸÁ¶¿¡¼­ À̽´°¡ µÉ¸¸ÇÑ ³»¿ëÀ» ÇÁ·Î±×·¥À¸·Î ±¸ÇöÇغ¸µµ·Ï ÇÑ´Ù. ( 2003/03/10 ) 151
Written by ienvyou - ÃÖÁö¿õ
1 of 1
 

MAKING DEEP COPIES OF OBJECTS

¿©±â¼­´Â "shallow" copy¶ó ºÒ¸®´Â Object.clone °ú ÀÚ½ÅÀÇ clone()¸Þ¼Òµå ¶Ç´Â »ç¿ëÀÚÁ¤ÀÇ clone() Á¶ÇÕ¿¡ ÀÇÇÑ object copy¸¦ À§ÇÑ ¹æ¹ýÀ» Á¤ÀÇÇÏ°Ú´Ù

¿¹Á¦·Î´Â ArrayList°´Ã¼¸¦ »ç¿ëÇÑ´Ù.
clone¸Þ¼Òµå¸¦ »ç¿ëÇϱâ À§ÇØ »õ·Î¿î ArrayList¸¦ »ý¼ºÇÏ°Ô µÉ°ÍÀ̸ç, ½ÇÁ¦ ¿øº» ¸®½ºÆ®ÀÇ ´Ù¾çÇÑ reference¸¦ copyÇÏ°Ô µÉ°ÍÀÌ´Ù.

°¢°¢ÀÇ ¿ø¼Ò°¡ º¯Çϱ⠽±°Å³ª, º¯ÇÒ¼ö ÀÖ´Â »óȲÀ̶ó¸é ±âÁ¸ÀÇ ¿øº» listµéÀº »õ·Î¿î ¸®½ºÆ®ÃøÀ¸·Î º¹»çµÇ¾î¾ß ÇÒ °ÍÀÌ´Ù. ´Ù¸¥ ¸»·Î Ç¥ÇöÇÏÀÚ¸é µÎ°³ÀÇ ArrayList°´Ã¼°¡  °øÀ¯ references¸¦ ÅëÇØ °øÅë¿ø¼Ò¸¦ °¡Áö°í Àִ°ÍÀÌ´Ù.
ÀÌ´Â ±×³É °øÀ¯°¡ µÇ´Â °ÍÀÌ ¾Æ´Ï¶ó list¸¦ copyÇÏ°íÀÚ ÇÒ¶§À̸ç,  ¸¸¾à list¸¦ copyÇϱ⸦ ¿øÇÑ´Ù¸é µÎ°³ÀÇ ¸®½ºÆ®¿¡ ´ëÇÑ °øÀ¯¸¦ °»½Å½ÃÅ°¸é ¾ÈµÈ´Ù.

ÀÌ·¯ÇÑ ´Ù¸¥ º¹»ç(cloneµî)µé°ú ±¸º°µÇ´Â ÃÑüÀûÀÎ ³»¿ëÀ» "deep copies"¶ó°í ºÎ¸¥´Ù.
¾Æ·¡´Â deep copy¿¡ ´ëÇÑ Å×Å©´ÏÄÃÇÑ ÇÁ·Î±×·¥À» º¸¿©ÁØ´Ù.

°¢°¢ÀÇ copy¸¦ 50000¹ø ¹Ýº¹½Ãµµ Çغ¸°Ú´Ù.

    import java.io.*;
    import java.util.*;
   
    class A implements java.io.Serializable {
        private int x;
   
        public A(int i) {
            setX(i);
        }
        public void setX(int i) {
            x = i;
        }
        public int getX() {
            return x;
        }
    }
   
    public class DeepDemo {
   
        // shallow copy¸¦ Á¤ÀÇÇß´Ù.
        static ArrayList copy1(ArrayList list) {
            return (ArrayList)list.clone();
        }
   
        // deep copy¸¦ Á÷Á¢ ÀÛ¼ºÇÑ °ÍÀÌ´Ù.
        static ArrayList copy2(ArrayList list) {
            ArrayList newlist = new ArrayList();
            A aobj = (A)list.get(0);
            newlist.add(new A(aobj.getX()));
            return newlist;
        }
   
        //  serialization¸¦ ÅëÇÑ deep copy¸¦ º¸¿©ÁØ´Ù.
        static ArrayList copy3(ArrayList list) throws Exception {
   
            // Á÷·ÄÈ­µÈ ArrayList¸¦ byte array·Î ³Ö´Â´Ù
   
            ByteArrayOutputStream baos =
                new ByteArrayOutputStream(100);
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(list);
            byte buf[] = baos.toByteArray();
            oos.close();
   
            // deserialize½ÃŲ byte array¸¦  ArrayList·Î ³Ö´Â´Ù.
   
            ByteArrayInputStream bais =
                new ByteArrayInputStream(buf);
            ObjectInputStream ois = new ObjectInputStream(bais);
            ArrayList newlist = (ArrayList)ois.readObject();
            ois.close();
   
            return newlist;
        }
   
        static final int NUMITERS = 50000;
   
        public static void main(String args[]) throws Exception {
            ArrayList listold, listnew;
            long currtime, elapsedtime2, elapsedtime3;
   
            // shallow copy¸¦ ½ÃÀÛÇÑ´Ù.
   
            listold = new ArrayList();
            listold.add(new A(37));
            listnew = copy1(listold);
            ((A)listold.get(0)).setX(47);
            System.out.print("copy1 old/new = ");
            System.out.print(((A)listold.get(0)).getX() + " ");
            System.out.println(((A)listnew.get(0)).getX());
   
            // Á÷Á¢ ÄÚµùÀ» ÅëÇÑ deep copy methodÀÇ È£Ãâ
   
            listold = new ArrayList();
            listold.add(new A(37));
            currtime = System.currentTimeMillis();
            for (int i = 1; i <= NUMITERS; i++) {
                listnew = copy2(listold);
            }
            elapsedtime2 = System.currentTimeMillis() - currtime;
            ((A)listold.get(0)).setX(47);
            System.out.print("copy2 old/new = ");
            System.out.print(((A)listold.get(0)).getX() + " ");
            System.out.println(((A)listnew.get(0)).getX());
   
            // Á÷·ÄÈ­¸¦ ÅëÇÑ deep copy
   
            listold = new ArrayList();
            listold.add(new A(37));
            currtime = System.currentTimeMillis();
            for (int i = 1; i <= NUMITERS; i++) {
                listnew = copy3(listold);
            }
            elapsedtime3 = System.currentTimeMillis() - currtime;
            ((A)listold.get(0)).setX(47);
            System.out.print("copy3 old/new = ");
            System.out.print(((A)listold.get(0)).getX() + " ");
            System.out.println(((A)listnew.get(0)).getX());
   
            System.out.println();
            System.out.println("copy2 time = " + elapsedtime2);
            System.out.println("copy3 time = " + elapsedtime3);
        }
    }

À§ÀÇ ÇÁ·Î±×·¥Àº ArrayList°¡ AŸÀÔÀÇ ¿ø¼ÒÇϳª¸¦ °¡Áö°í »ç¿ëÇÑ ÇÁ·Î±×·¥ÀÌ´Ù. AŸÀÔÀÇ °´Ã¼´Â setX() ¸Þ¼Òµå¸¦ ÅëÇÏ¿© °ªÀÌ º¯ÇÏ°Ô µÈ´Ù.

ÀÌ ÇÁ·Î±×·¥Àº 3°¡ÁöÀÇ copy¸Þ¼Òµå¸¦ º¸¿©ÁÖ°í Àִµ¥ copy1Àº ArrayList.clone()¸Þ¼Òµå¸¦ ÀÌ¿ëÇÑ shallow copy¸¦ º¸¿©ÁÖ°í ÀÖ´Ù.
ÀÌ ¸Þ¼Òµå´Â »õ·Î¿î ¸®½ºÆ®¸¦ »ý¼ºÇÑÈÄ¿¡ ±âÁ¸ÀÇ list°¡ ÂüÁ¶ÇÏ°í ÀÖ´Â object¸¦ º¹»çÇÑ´Ù.

ÀÌÈÄ¿¡ copy1¸Þ¼Òµå°¡ È£ÃâµÇ´Âµ¥, ÇÁ·Î±×·¥¿¡¼­´Â old listÀÇ ÇÑ ¿ø¼ÒÀÇ °ªÀÌ ¹Ù²î°í, »õ·Î¿î list¾ÈÀÇ ¿ø¼Ò°ªÀ» üũÇÔÀ¸·Î¼­ ±× »õ·Î¿î listÀÇ °ªÀÌ ¹Ù²î°Ô²û ÄÚµùÇÏ¿´´Ù.¿©±â¼­ÀÇ »õ·Î¿î list·Î º¯ÇÏ´Â °ÍÀº shallow copy(clone¸Þ¼Òµå¸¦ ÅëÇÑ)¸¦ °¡¸£Å²´Ù.

copy2¸Þ¼Òµå´Â deep copy¸¦ ¸¸µé±â À§ÇØ Á÷Á¢ ÀÛ¼ºÇÑ Äڵ带 ÀÌ¿ëÇÏ¿© Á¢±ÙÀ» ½ÃµµÇÏ°í Àִµ¥, »õ·Î¿î ArrayList¸¦ ÁغñÇÏ°í ±âÁ¸ÀÇ list·ÎºÎÅÍ ¿ø¼Ò°ªÀ» ¾ò¾î¿ÂÈÄ¿¡ »õ·Î¿î list¿¡ ±âÁ¸¿¡ »ý¼ºµÈ ¿ø¼Ò°ªÀ» Á÷Á¢ add½ÃÅ°´Â °ÍÀ» º¸¿©ÁÖ°í ÀÖ´Ù.

copy3¿¡¼­´Â serializationÀ» »ç¿ëÇß´Ù. ArrayList¸¦ byte array·Î º¯È¯ÇÑ ÈÄ¿¡ ByteArrayOutputStream object¸¦ ÀÌ¿ëÇÏ¿© ÀúÀå½ÃŲÈÄ, ´Ù½Ã ¿ªÀ¸·Î ÇÁ·Î¼¼½º¸¦ µ¹·Á ArrayList·Î °ªÀ» ¾ò¾î³»¾ú´Ù.

ÀÌ ÇÁ·Î±×·¥À» ½ÇÇà½ÃÄ×À»¶§ ¾Æ·¡ÀÇ °á°ú°ªÀ» È®ÀÎÇÒ¼ö ÀÖ´Ù.

    copy1 old/new = 47 47
    copy2 old/new = 47 37
    copy3 old/new = 47 37

    copy2 time = 47
    copy3 time = 8500

hand-codingÇÑ deep copyingÀÎ copy2¿Í serializationÀ» »ç¿ëÇÑ copy3´Â È®¿¬È÷ ±¸ºÐÀÌ µÇ°í ÀÖ´Ù. ¿©±â¼­ÀÇ perfomance´Â ¸í¹éÇÑ issueÀÌ´Ù.
serializationÁ¢±ÙÀº hand-codingÇÑ°Í¿¡ ºñÇØ ´«¿¡ ¶ç°Ô ´À¸°°ÍÀ» È®ÀÎÇÒ¼ö ÀÖÀ»°ÍÀÌ´Ù.
¾î·µç deep copies¸¦ ÇÒ¶§´Â serializationÀ» ÇÇÇÏ´Â °ÍÀÌ ¹Ù¶÷Á÷ÇÒ°ÍÀÌ´Ù.


À§ Äڵ带 º¸¸é ¾Ë°ÚÁö¸¸ ½ÃµµÈ½¼ö´Â 50000¹øÀÌ´Ù. ÇϳªÀÇ °´Ã¼¿¡ ´ëÇÑ copy¸¸ ÇϹǷΠ±×¸® ¿À·£ ½Ã°£ÀÌ °É¸®Áö ¾ÊÁö¸¸, ÀÌ·¯ÇÑ ½Ã°£Àº copy¸¦ ÀÌ¿ëÇÏ¿© ¾ÖÇø®ÄÉÀ̼ÇÀÇ criticalÇÑ ¿ä¼Ò¿¡´Â Àû¿ëÀ» ½Ãų¼ö ¾øÀ» °ÍÀÌ´Ù.

¿©±â¼­ °¡Àå Å« À̽´¸¦ Ç×»ó ±×·¡¿ÔµíÀÌ ¼º´ÉÀÌ´Ù.Æ®¸®±¸Á¶³ª ±×·¡ÇÁ µîÀÇ ÀڷᱸÁ¶¿¡ À־ deep copy¸¦ »ç¿ëÇϱ⠿øÇÑ´Ù¸é ÇÁ·Î±×·¥ÀÌ ³Ê¹« º¹ÀâÇØÁú¼ö ÀÖ´Ù

¶ÇÇÑ serialization¿¡ ÀÇ ÇÑ Á¢±ÙÀº ±× ºñ¿ë°ú ½Ã°£¿¡ À־ °í·ÁÇغ¸¾Æ¾ß ÇÒ ¹®Á¦ÀÌ´Ù.


 

Written by Carouser : 2001-05-30
 
1
References
 
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD