Aerial warfare over planet Phillon - a fast-paced battle for territory, honour and reputation. Sophisticated strategies and daring manoeuvres are all in a day’s work for these pilots. Fight as one of them and defend your nation!
A friend just rrereefd me to this page and I think that the test scenario in play here is getting optimized by the compiler as you are not really doing anything with the values in the array for the for loop case. I condensed your test to just working with the for and for each for an array and the only thing I modified was change a = ? to a += ? . I ended up getting very different results where the foreach was actually much faster. I think what was happening in your test case was that the value from the array was not actually getting retrieved in the for loop as it was getting optimized by the compiler.const SIZE:uint = 6000;const REPS:uint = 2000; var i:int;var s:String;var a:int = 0;var b:String;var rep:int;var res:String = "For-each times:\n";var before:uint;var after:uint;var arr:Array = new Array SIZE ; for i = 0; i < SIZE; ++i arr i = int Math.random *1000 ; before = getTimer ;for rep = 0; rep < REPS; ++rep for each i in arr a += i; after = getTimer ;res += "\tArray: " + after-before + "\n"; res += "For times:\n"; a = 0; before = getTimer ;for rep = 0; rep < REPS; ++rep for i = 0; i < SIZE; ++i a += arr i ; after = getTimer ;res += "\tArray: " + after-before + "\n"; trace res ;For-each times: Array: 2603For times: Array: 6313
Hozzászólásokat csak regisztrált felhasználók írhatnak. [ Regisztráció | Belépés ]