# PureBasic Forum

 It is currently Tue May 21, 2013 6:04 am

 All times are UTC + 1 hour

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: speed up codePosted: Thu Mar 01, 2012 6:46 pm
 Enthusiast

Joined: Fri Mar 27, 2009 9:41 am
Posts: 333
Location: Athens, Greece
My question may be a little stupid so I apologise from now.
I have the following code:
Code:
Dim temp.b(ArraySize.l)
For i = 1 To 8
For ii= 1 To ArraySize
temp(ii) = Random(255)
Next
....
Next

to create an array with random data 8 times. The code is a little slow when ArraySize is a number like 1,000,000.
I know that this is obvious when we call Random() so many times but is there any way to speed up?

_________________

Top

 Post subject: Re: speed up codePosted: Thu Mar 01, 2012 6:55 pm

Joined: Tue Dec 23, 2003 3:54 am
Posts: 932
Location: New York
Try this:

Code:
Dim temp.b(ArraySize.l)
For i = 1 To 8
RandomData( @temp(0), ArraySize )
....
Next

You can only use this trick because you random range is 0-255... all 8 bits of every single byte.

If you were using something like temp(ii) = Random(100) then it wouldn't work correctly.

Top

 Post subject: Re: speed up codePosted: Thu Mar 01, 2012 7:32 pm
 Enthusiast

Joined: Fri Mar 27, 2009 9:41 am
Posts: 333
Location: Athens, Greece
With your code, if ArraySize is 30,000,000 then the time needed to operate is not more than 330 milliseconds.

That was what I needed!! Thanx a lot!!

_________________

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: Exabot [Bot] and 3 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite