最近有朋友讨论说for循环里不要用list.size(),这样会导致每次都调用一次list.size()接口会耗性能,建议我们在外面使用一个变量来存储这个list.size()的值再放到循环里面,真的是这样吗?我做了个小测试,代码如下:
public class Test2 {
public static void main(String[] args) {
List list = new ArrayList();
list.add("1");
list.add("2");
for (int i = 0; i < list.size(); i++) {
System.out.println(i);
}
}
}
这里将list.size()放在了for循环里,按朋友的说法,这里应该会调用多次。
接下来我查看了字节码发现,这里确实会调用多次list.size()方法,字节码如下:
具体字节码指令解析可查看:
接着查看了ArrayList调用size()方法的源码是直接返回一个int 型的 size变量的,而这个size变量是每次新增和删除都会改变这个size的值
结论:经过网友们热情的探讨与更正,for循环里会多次调用list.size(),list.size()方法会直接返回一个声明在类中的size全局变量,虽然这个size会直接保存在堆中,但每次发生函数调用确实会发生压栈出栈的性能开销。
http://groups.tianya.cn/list-59073-1.shtml
http://groups.tianya.cn/m/list-59073-1.shtml
http://groups.tianya.cn/post-59073-7ed4d0e54a714a2e845806fb8af39fc8-1.shtml
http://groups.tianya.cn/post-59073-f43607c2741748e4becb8a39e7aa9edb-1.shtml
http://groups.tianya.cn/post-59073-3da85bf5e611485093b6a8c7485148ba-1.shtml
http://groups.tianya.cn/post-59073-ed415b8563f14cd49164af094f4f7812-1.shtml
http://groups.tianya.cn/post-59073-e90ba35fc5384d4fa4e995b6b9ce291f-1.shtml
http://groups.tianya.cn/post-59073-e3e6e678220f41309c7554c4dcd5cd17-1.shtml
http://groups.tianya.cn/post-59073-1edf6dd608fc4fc78e22ea96cec8237e-1.shtml
http://groups.tianya.cn/post-59073-bb603a648e3b43ecbabc721549de4646-1.shtml
http://groups.tianya.cn/post-59073-46eaba4779cb49b88f47a03b75d06999-1.shtml
http://groups.tianya.cn/post-59073-7c97f48319db4f60ac5c0be41d316e8b-1.shtml
http://groups.tianya.cn/post-59073-3c99730a7649417d9d0584e196311092-1.shtml
http://groups.tianya.cn/post-59073-f923f1b2d6c14f179962ec67b454a2fd-1.shtml
http://groups.tianya.cn/post-59073-a6537b0979d244c0b1b814a36012abd9-1.shtml
http://groups.tianya.cn/post-59073-4a7c43285f504bc2a8db4983c3858fb8-1.shtml
http://groups.tianya.cn/post-59073-950973c618054fbcb74bc9b0392c43d0-1.shtml
http://groups.tianya.cn/post-59073-a064e7daf551435ca36cacd5d886e2a3-1.shtml
http://groups.tianya.cn/post-59073-645fbe17530349c0bd18b6c9518ea99c-1.shtml
http://groups.tianya.cn/post-59073-0b2ca45ed8a84ddeaf87d721d2ad53da-1.shtml
http://groups.tianya.cn/post-59073-a5a700d5820043aa8e9a4673127c88b8-1.shtml
http://groups.tianya.cn/post-59073-4c4e8400bab7483dadff834635c7afd7-1.shtml
http://groups.tianya.cn/post-59073-0dca9687ee3041a4ad7e67233f58ea11-1.shtml
http://groups.tianya.cn/post-59073-3f9706e38f4542318c7ba0cb6312234b-1.shtml
http://groups.tianya.cn/post-59073-53d88a32cdd6449b9a3da68a37e815f0-1.shtml
http://groups.tianya.cn/post-59073-e5d572926a3243e2970d21eddfff0bd2-1.shtml
http://groups.tianya.cn/post-59073-6bdb68d7c45841b9913363f523da8a76-1.shtml
http://groups.tianya.cn/post-59073-5d7fb977beac43c49d0f30eea7e4de60-1.shtml
http://groups.tianya.cn/post-59073-d3697ea5481a493989884655cb978c15-1.shtml
http://groups.tianya.cn/post-59073-965d364d012542d0ba6021c490e45fc6-1.shtml
http://groups.tianya.cn/post-59073-9ea7cd7497da491a9be19fba6619d9e0-1.shtml
http://groups.tianya.cn/post-59073-7163af2ca26b48e580f194d893ca5a8c-1.shtml
http://groups.tianya.cn/post-59073-af8afb9de8814f1eabeaf22712bb84f0-1.shtml
http://groups.tianya.cn/post-59073-4c9e7b54da624221b9c62e499fb29676-1.shtml
http://groups.tianya.cn/post-59073-6674a61c7253462bbdb7cb20fe9ee45e-1.shtml
http://groups.tianya.cn/post-59073-7977d7d6c83c4daca83f7a0abc28856e-1.shtml
http://groups.tianya.cn/post-59073-e2509aeb5c674a6b8da527397cb03ccb-1.shtml
http://groups.tianya.cn/post-59073-8d33683a7877416e87c1bb6d3e5788b0-1.shtml
http://groups.tianya.cn/post-59073-8496425904e94172bc8fda904c280f53-1.shtml
http://groups.tianya.cn/post-59073-146bb48eb4144482abeebde8e2d89963-1.shtml
http://groups.tianya.cn/post-59073-b8ea562b57bb45b38e868e4b374eb372-1.shtml
http://groups.tianya.cn/post-59073-58987b3ff69a44209e64d2e5d441d51b-1.shtml
http://groups.tianya.cn/post-59073-ca2277ca02244bf08627b2b246f49c8d-1.shtml
http://groups.tianya.cn/post-59073-20afb801d7584ba2a0c9c61ba95593b3-1.shtml
http://groups.tianya.cn/post-59073-01c3f3cd115e485d9746ec8cd64f025b-1.shtml
http://groups.tianya.cn/post-59073-a0df3c3a69624b38a8711d2d316cc758-1.shtml
http://groups.tianya.cn/post-59073-07806ea8e6a749efb7cfa61c4e8dd8ed-1.shtml
http://groups.tianya.cn/post-59073-926d5ccba93b4c2e93236c9e33a1d0a8-1.shtml
http://groups.tianya.cn/post-59073-b27fcd783f9d4b91b648bdfc45eeeb49-1.shtml
http://groups.tianya.cn/post-59073-ea5f1c9910354ebeaea43606d303184d-1.shtml
http://groups.tianya.cn/post-59073-ef085fc7e8584669a16f9a709e4f7472-1.shtml
http://groups.tianya.cn/post-59073-b49e60f24864414aa46a214c17ac1c0a-1.shtml
http://groups.tianya.cn/post-59073-b98427c08ad1495aae6f9be369e3aa28-1.shtml
http://groups.tianya.cn/post-59073-3c20d52fb00b47beb4b43c6ca47c10ec-1.shtml
http://groups.tianya.cn/post-59073-87756a57092143c0adbb4054136b60a4-1.shtml
http://groups.tianya.cn/post-59073-03a96db407eb425781b6bacd2da221eb-1.shtml
http://groups.tianya.cn/post-59073-5a55bce5ef514fafb52e7baf5b1bff94-1.shtml
http://groups.tianya.cn/post-59073-1a320d8c35984656b2627bf512a5e84b-1.shtml
http://groups.tianya.cn/post-59073-aea2d67ead494cd99de87e90a4a0b707-1.shtml
http://groups.tianya.cn/post-59073-df404871e579425fb75aff871e67469f-1.shtml
http://groups.tianya.cn/post-59073-e6d28ce33a4d4f97825f07f19b0a43dc-1.shtml
http://groups.tianya.cn/post-59073-a4b4c8f6fc504c6e9ee58f8c7810f220-1.shtml
http://groups.tianya.cn/post-59073-a2b89f4ed7ba48938a6cf8f39d72bd50-1.shtml
http://groups.tianya.cn/post-59073-3b6a1d50bcc5411dad8dcc19965a9d55-1.shtml
http://groups.tianya.cn/post-59073-b255275221124c24a9a116439c263c2c-1.shtml
http://groups.tianya.cn/post-59073-cf5fe2c80a1e45799b407527cea191c5-1.shtml
http://groups.tianya.cn/post-59073-5e8d7f1baa294bd490736977c7df4224-1.shtml
http://groups.tianya.cn/post-59073-f1349de5b2244cea9b6d518edd07a8bd-1.shtml
http://groups.tianya.cn/post-59073-d55286c750e3406b82561c748474e62e-1.shtml
http://groups.tianya.cn/post-59073-af55c082318b41bfa3486fe003dfd5b6-1.shtml
http://groups.tianya.cn/post-59073-70db2cfc58c04571881cb4726a5a8a7b-1.shtml
http://groups.tianya.cn/post-59073-42293674d5d14beca0e097ceb094441f-1.shtml
http://groups.tianya.cn/post-59073-2e93215e8365432593d91ec09fcf6460-1.shtml
http://groups.tianya.cn/post-59073-3852adadefaa44909608ff6814be4dd3-1.shtml
http://groups.tianya.cn/post-59073-e2a6718db9f047bbb24ed8051f210a84-1.shtml
http://groups.tianya.cn/post-59073-9200bcd5710e42a49d7f84e45be400aa-1.shtml
http://groups.tianya.cn/post-59073-0faf3e8cc28c4fc3bce5c16cde1d0be3-1.shtml
http://groups.tianya.cn/post-59073-e04106519b1e4f4cb1936c02cee162f8-1.shtml
http://groups.tianya.cn/post-59073-5498bd041c3e4ff894c5dc1130afd299-1.shtml
http://groups.tianya.cn/post-59073-01d6d4ba7ce649e3ac62cc1ed9ff378f-1.shtml
http://groups.tianya.cn/post-59073-f5987b7b06814b0bb750f2835c001cb1-1.shtml
http://groups.tianya.cn/post-59073-74479ba6dcfc424f811fed6fe361bdbe-1.shtml
http://groups.tianya.cn/post-59073-b90969d1168547ffab507e136e9181aa-1.shtml
http://groups.tianya.cn/post-59073-32d8b8da83354ab8945e5c3095b47767-1.shtml
http://groups.tianya.cn/post-59073-664e02addf634a3bad36a0fbc9bd6aa2-1.shtml
http://groups.tianya.cn/post-59073-5b2a6583c2ed4bf899e14ac62c56af29-1.shtml
http://groups.tianya.cn/post-59073-55844694c66340bdb08cfbd972fc1fe0-1.shtml
http://groups.tianya.cn/post-59073-424e8dd8060f46ae98bb4105c91ea752-1.shtml
http://groups.tianya.cn/post-59073-2e94d7980f164979810e7ce2e89f8ddc-1.shtml
http://groups.tianya.cn/post-59073-ee7e6f4fee904357b1ed022cf39c63d4-1.shtml
http://groups.tianya.cn/post-59073-9e4269d4bbd846f48fc91ed93ef3861f-1.shtml
http://groups.tianya.cn/post-59073-1afbdfd3908d4b43988ef4e062874b9f-1.shtml
http://groups.tianya.cn/post-59073-e351f25af5c6436db63e0e03e930a408-1.shtml
http://groups.tianya.cn/post-59073-82ace1f95cad4addad22d5727f6d1234-1.shtml
http://groups.tianya.cn/post-59073-9a198358ac884b42b859c1c0bc5923dd-1.shtml
http://groups.tianya.cn/post-59073-6408a8100d024e1aa25f7ac35f6829ad-1.shtml
http://groups.tianya.cn/post-59073-1263785152a3447b93c91fdbbb4384f9-1.shtml
http://groups.tianya.cn/post-59073-6aed1d9a80b748f597dc644def0a0dd5-1.shtml
http://groups.tianya.cn/post-59073-bdfd990e1ec44d98b243d6f95851e8cb-1.shtml
http://groups.tianya.cn/post-59073-fff1e41ba8cf4a88b4ff3ff71d4d396f-1.shtml
http://groups.tianya.cn/post-59073-047d17f25bc94b90a70e42637238beb4-1.shtml
http://groups.tianya.cn/post-59073-a1202aecceb543aca78ffdb8af7cf1c4-1.shtml
http://groups.tianya.cn/post-59073-275a049b33a94d94903013c738d0678c-1.shtml
http://groups.tianya.cn/post-59073-8a76cb4ab0c64a1bb48bdf15ee60abec-1.shtml
http://groups.tianya.cn/post-59073-4adcd5cec6ff4f24ae9faa17065c0d27-1.shtml
http://groups.tianya.cn/post-59073-10ac80acc6cf44898d36784ffad48b81-1.shtml
http://groups.tianya.cn/post-59073-41d1077b27d34c91a5d5ba466c62ba3a-1.shtml
http://groups.tianya.cn/post-59073-c8f2d8249eeb43d5aaee25991b3549ad-1.shtml
http://groups.tianya.cn/post-59073-052122789171432890a949f7f2c972da-1.shtml
http://groups.tianya.cn/post-59073-f1b9f767b95140c2a23830cf168b4739-1.shtml
http://groups.tianya.cn/post-59073-1b6a345a0b9a4ba4b0385c72964c3d05-1.shtml
http://groups.tianya.cn/post-59073-8fadc36271a6400ca1b2403bd12ae7d5-1.shtml
http://groups.tianya.cn/post-59073-39eb75487f35451da89820f4bbbcc1f0-1.shtml
http://groups.tianya.cn/post-59073-a4d2ff63e5114c83980b50968325d437-1.shtml
http://groups.tianya.cn/post-59073-7c3b8c8add094e0d839e34ac1e8fa341-1.shtml
http://groups.tianya.cn/post-59073-d1ef5e50ab1344eab15401fc190cfebb-1.shtml
http://groups.tianya.cn/post-59073-c169cb24964849d18a682214c114c8f5-1.shtml
http://groups.tianya.cn/post-59073-449ce9cae6cc40c389bfb970cc651fad-1.shtml
http://groups.tianya.cn/post-59073-bae264566ce04fbe8d644bcfafaf8496-1.shtml
http://groups.tianya.cn/post-59073-92a04db3d9b444b9bdd01dc50dd1d7ab-1.shtml
http://groups.tianya.cn/post-59073-856578baeb54432ab8efd46c4828336a-1.shtml
http://groups.tianya.cn/post-59073-a5990a05c36e45db8ff6d969f9522e20-1.shtml
http://groups.tianya.cn/post-59073-d6931f9e241d4cfcac9fdd0e22ad4733-1.shtml
http://groups.tianya.cn/post-59073-75ca23a58f2c47559032357a91b00c66-1.shtml
http://groups.tianya.cn/post-59073-3c5bf8cd86bd4b66944efa99b72409c4-1.shtml
http://groups.tianya.cn/post-59073-3dd06d07158c4997ab011226a8ee7aeb-1.shtml
http://groups.tianya.cn/post-59073-42206e57774c4aa7b52c67699ea40cba-1.shtml
http://groups.tianya.cn/post-59073-47507ee9402d4c2c9f22285456fed2e5-1.shtml
http://groups.tianya.cn/post-59073-c707d189bc7d465ca3146e5ae7af929d-1.shtml
http://groups.tianya.cn/post-59073-98a0403bb75c4d0b846623770f80d435-1.shtml
http://groups.tianya.cn/post-59073-fc6d37f6d8d443e3b9cbdab6504e3bd1-1.shtml
http://groups.tianya.cn/post-59073-bfd7414604bd4a07ac6765ffdef0ce13-1.shtml
http://groups.tianya.cn/post-59073-d119ffb55f9a41bbb7beca5c438ccafa-1.shtml
http://groups.tianya.cn/post-59073-3b16d90d4ff643a2b0dfb76045a497e0-1.shtml
http://groups.tianya.cn/post-59073-4e8404e1ce87452fab07a8ca0edc7588-1.shtml
http://groups.tianya.cn/post-59073-41707d780dfc44df9498c46a795acff2-1.shtml
http://groups.tianya.cn/post-59073-2aaf8558b3cf48c0871f6fab68663d7b-1.shtml
http://groups.tianya.cn/post-59073-dabb84f2594e47438588b129753f335f-1.shtml
http://groups.tianya.cn/post-59073-cb03dbaed0314ad182cc76acc1e3efcb-1.shtml
http://groups.tianya.cn/post-59073-e3c39be0a62e4e338c94e8b499077781-1.shtml
http://groups.tianya.cn/post-59073-67a4d217247e4c96bdc65a06fb2d6f08-1.shtml
http://groups.tianya.cn/post-59073-6977229164484150b803e8b2afa00323-1.shtml
http://groups.tianya.cn/post-59073-ffd88d1d56b240399232d37b0e93336c-1.shtml
http://groups.tianya.cn/post-59073-c9fd8171719a4ea6bf31034aad578342-1.shtml
http://groups.tianya.cn/post-59073-17fa18d315cb44faaf545e1494b6d65c-1.shtml
http://groups.tianya.cn/post-59073-5d493c08819c4b73ab3d1d9a179adc7b-1.shtml
http://groups.tianya.cn/post-59073-9f74d975e97642fd8de2906d839ab469-1.shtml
http://groups.tianya.cn/post-59073-7e1c906e7a0846d8906cb070d1b8ad49-1.shtml
http://groups.tianya.cn/post-59073-c468d0ed82f64c1ba91ef97cda005efd-1.shtml
http://groups.tianya.cn/post-59073-44300689723d445799ecb1e73a02c013-1.shtml
http://groups.tianya.cn/post-59073-5908889708d443c0a2fca50ad7563ab4-1.shtml
http://groups.tianya.cn/post-59073-d05a001d1b754dba9e2e64335bf73c86-1.shtml
http://groups.tianya.cn/post-59073-a1ccd988d6be47f293e4e4f4fcf82045-1.shtml
http://groups.tianya.cn/post-59073-2be413ba30cf4c7c997d547b7b6d8b15-1.shtml
http://groups.tianya.cn/post-59073-1e3908902b5e4cd6a825130336f56dc9-1.shtml
http://groups.tianya.cn/post-59073-332f3467dfcc44feb057ce4467e30790-1.shtml
http://groups.tianya.cn/post-59073-0b92f338a9444766944d2c6241c08542-1.shtml
http://groups.tianya.cn/post-59073-6afc639ac3374a4eb3201034bef0e79c-1.shtml
http://groups.tianya.cn/post-59073-c7a5cfa23f7b4447b6fe3ed14caeff7d-1.shtml
http://groups.tianya.cn/post-59073-a7869eca374c4e3ba014469af53a5550-1.shtml
http://groups.tianya.cn/post-59073-d92e9b1381134fd88d36486be36ab9d0-1.shtml
http://groups.tianya.cn/post-59073-bcb29a8a38ce4d61b7b4bc6018c0905c-1.shtml
http://groups.tianya.cn/post-59073-29b7f3459ce54ee8920a0425e44ff86b-1.shtml
http://groups.tianya.cn/post-59073-429b8d82aa2d4e54a2918d1d7be94d36-1.shtml
http://groups.tianya.cn/post-59073-c432d3b332e141a8be35ad3f4127de6e-1.shtml
http://groups.tianya.cn/post-59073-eb72500307204f22ab2f2f00848aea1e-1.shtml
http://groups.tianya.cn/post-59073-63fa561cd4bf477cbb04bd2ec00f5134-1.shtml
http://groups.tianya.cn/post-59073-f738e11b6b174d3bae6e6f0da3980302-1.shtml
http://groups.tianya.cn/post-59073-731fbadc76d24df7b5dcd4d2e51ce1ff-1.shtml
http://groups.tianya.cn/post-59073-416135334d7c446aa4707cc57f00088a-1.shtml
http://groups.tianya.cn/post-59073-ad2f92c667aa432f922d39aa8511d02c-1.shtml
http://groups.tianya.cn/post-59073-1da88c830c5a49fea7796b45d6129274-1.shtml
http://groups.tianya.cn/post-59073-53f844f1dc0c4115a1be0c999d60059e-1.shtml
http://groups.tianya.cn/post-59073-aa0ab53803aa461683cee1dc1ce77fd8-1.shtml
http://groups.tianya.cn/post-59073-0784ef79a1f1424e9d922cd1d5cae26a-1.shtml
http://groups.tianya.cn/post-59073-a15d8a8b8ede4757a160298031b9c2d3-1.shtml
http://groups.tianya.cn/post-59073-b5c63042029f48c89111fafaba028558-1.shtml
http://groups.tianya.cn/post-59073-e78e6adeedd640dba12e69e4959dbf73-1.shtml
http://groups.tianya.cn/post-59073-43e70d6eaf9f4175b6726b8974ead999-1.shtml
http://groups.tianya.cn/post-59073-00fc3bc13ae5476eb7e167ebba85e957-1.shtml
http://groups.tianya.cn/post-59073-e8f81444bcd24da197fb194188b9054d-1.shtml
http://groups.tianya.cn/post-59073-b3bc0a4ff609451ca4b0c52505b3dc47-1.shtml
http://groups.tianya.cn/post-59073-e76a3a733a054602b961c1d25c4535f6-1.shtml
http://groups.tianya.cn/post-59073-0c48e13b1cb04b7c84480bc1179bf1ee-1.shtml
http://groups.tianya.cn/post-59073-fe589b82709445109cbe6741fdf1d237-1.shtml
http://groups.tianya.cn/post-59073-5edd8783de054d9fbff7287acd95b393-1.shtml