From: Ryan Davis Date: 2012-09-27T09:13:05+09:00 Subject: Re: Array Creation On Sep 26, 2012, at 12:21 , Tridib Bandopadhyay wrote: > I was wondering why Ruby takes long time to create an array. I tried: > > Start = Time.now > names = Array.new(614400000) > puts Time.now-Start > > Ruby took around 308 seconds to create this large array. Can anyone tell > me why Ruby takes such a long time? Also when assigning a variable to > the array takes long time. Why is that? That's invariably swap... MUUUCH slower to access a disk than memory. On a machine with enough memory it takes about the same amount of time to do equivalent work: % /usr/bin/time -l ruby19 -e 'Array.new(614400000)' 2.74 real 0.88 user 1.82 sys 4919554048 maximum resident set size ... 0 swaps ... % cc -O3 -Wall -pedantic -std=c99 woot.c && /usr/bin/time -l ./a.out 2.92 real 1.22 user 1.69 sys 4915695616 maximum resident set size ... 0 swaps ... where woot.c is: typedef unsigned long VALUE; int main() { size_t count = 614400000; VALUE * names = (VALUE*)malloc(count * sizeof(VALUE)); for(int i = 0; i < count; i++) { names[i] = 4; } free(names); }