# Big empty file creation

**URL:** <https://www.ruby-forum.com/t/big-empty-file-creation/141651>\
**Category:** Ruby\
**Created:** [June 28, 2008, 11:00am UTC](https://www.ruby-forum.com/t/big-empty-file-creation/141651 "2008-06-28T11:00:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zhukov\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e95f7d/32.png) [@Zhukov\_P](https://www.ruby-forum.com/u/Zhukov_P)\
**Post date:** [June 28, 2008, 11:00am UTC](https://www.ruby-forum.com/t/big-empty-file-creation/141651/1 "2008-06-28T11:00:21Z")

</div>

I need to creat an empty file, over 10GB size.  
Which way is fastest in file creation?

* * *

## file\<\<“0”\*file\_size\_in\_bytes

consme too much memory

---

<div class="post-metadata">

**Author:** ![Zhukov\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e95f7d/32.png) [@Zhukov\_P](https://www.ruby-forum.com/u/Zhukov_P)\
**Post date:** [June 28, 2008, 11:20am UTC](https://www.ruby-forum.com/t/big-empty-file-creation/141651/2 "2008-06-28T11:20:15Z")

</div>

ts wrote:

> Zhukov P. wrote:
> 
> > I need to creat an empty file, over 10GB size.  
> > Which way is fastest in file creation?
> 
> use IO#sysseek followed which a IO#seekwrite  
> ^^^^^^^^^^^^  
> IO#syswrite

Sorry,

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Zhukov\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e95f7d/32.png) [@Zhukov\_P](https://www.ruby-forum.com/u/Zhukov_P)\
**Post date:** [June 28, 2008, 11:27am UTC](https://www.ruby-forum.com/t/big-empty-file-creation/141651/3 "2008-06-28T11:27:16Z")

</div>

On 28.06.2008 11:10, ts wrote:

> Zhukov P. wrote:
> 
> > I need to creat an empty file, over 10GB size.  
> > Which way is fastest in file creation?
> 
> use IO#sysseek followed which a IO#seekwrite

But note that this will not work on all OS in case the file must be  
allocated. Seeking likely creates a sparse file.

Another approach is to use dd like

dd if=/dev/zero of=your\_file bs=1048576 count=10240

That’s probably as fast as it gets if you need blocks actually allocated  
to the file.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Zhukov\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e95f7d/32.png) [@Zhukov\_P](https://www.ruby-forum.com/u/Zhukov_P)\
**Post date:** [June 28, 2008, 11:13am UTC](https://www.ruby-forum.com/t/big-empty-file-creation/141651/4 "2008-06-28T11:13:13Z")

</div>

Zhukov P. wrote:

> I need to creat an empty file, over 10GB size.  
> Which way is fastest in file creation?

use IO#sysseek followed which a IO#seekwrite

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Zhukov\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e95f7d/32.png) [@Zhukov\_P](https://www.ruby-forum.com/u/Zhukov_P)\
**Post date:** [June 28, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/big-empty-file-creation/141651/5 "2008-06-28T13:42:22Z")

</div>

On 28 Jun 2008, at 09:57, Zhukov P. wrote:

> ## file\<"0"file\_size/x end
> 
> too slow ☹

Pick a value of x that’s a multiple of 512 bytes as this will match  
your data writes to the OS’s disk cache. This may or may not gain you  
some performance advantage which will vary depending on the underlying  
cluster size of the file system, general filesystem overhead, disk  
fragmentation and a host of other properties that our outside your  
control as a programmer. Even coding is assembler creating a 10GB file  
with each byte zeroed is going to be a slow process…

Ellie

## Eleanor McHugh Games With Brains [http://slides.games-with-brains.net](http://slides.games-with-brains.net)

raise ArgumentError unless @reality.responds\_to? :reason

---

<div class="post-metadata">

**Author:** ![Zhukov\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e95f7d/32.png) [@Zhukov\_P](https://www.ruby-forum.com/u/Zhukov_P)\
**Post date:** [June 28, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/big-empty-file-creation/141651/6 "2008-06-28T14:06:56Z")

</div>

On 28.06.2008 12:23, Zhukov P. wrote:

> > Another approach is to use dd like
> > 
> > dd if=/dev/zero of=your\_file bs=1048576 count=10240
> > 
> > That’s probably as fast as it gets if you need blocks actually allocated to  
> > the file.

> i can’t use dd, cause i want a cross-platform application

dd runs on my Linux, cygwin, Solaris, HP UX… - pretty cross platform  
I’d say. 🙂

The question is - do you need the whole file to be allocated or not? If  
yes, a solution is slow regardless of programming language.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![Zhukov\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e95f7d/32.png) [@Zhukov\_P](https://www.ruby-forum.com/u/Zhukov_P)\
**Post date:** [June 28, 2008, 12:26pm UTC](https://www.ruby-forum.com/t/big-empty-file-creation/141651/7 "2008-06-28T12:26:55Z")

</div>

On Sat, Jun 28, 2008 at 1:22 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> allocated. Seeking likely creates a sparse file.  
> robert

i can’t use dd, cause i want a cross-platform application
