By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

How can I make this code to go faster???

P: n/a
This is the code I have so far, to generate 50.000 random numbers.
This code will generate the same sequence every time I run it. But the
problem being is that it's slow. It take on my p4 1.6gh almost 10
seconds to create 50.000 rundom numbers. This is just a 50k text in
other words.
Someone claimed in sciencegroups.com that he could create 100 milion
in 7 seconds on similar computer.
I need to speed this code somehow.
Could someone please look at it and help me to revise it?
Thanks a lot.
Sincerely,
Martin

Here is the code:

Private Const RAND_SEED As Integer = 123 ' Or anything
Private Const MAX_VALUE As Integer = 1000 ' This assumes single
digits

Private Sub Button1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button1.Click
TextBox1.Text = ""
Button1.Visible = False
Dim numero As String
Dim r As New Random(RAND_SEED)
For i As Integer = 1 To 50000
numero += r.Next(MAX_VALUE).ToString
' StatusBar1.Text = i
Next i
TextBox1.Text = numero
Button1.Visible = True
End Sub

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 20 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
well,

I would look into the stringbuilder class. The string class is not very
optimized for appending. This may speed your code up.

Chris

"Martin Ho" <ma******@74tech-dot-com.no-spam.invalid> wrote in message
news:40**********@Usenet.com...
This is the code I have so far, to generate 50.000 random numbers.
This code will generate the same sequence every time I run it. But the
problem being is that it's slow. It take on my p4 1.6gh almost 10
seconds to create 50.000 rundom numbers. This is just a 50k text in
other words.
Someone claimed in sciencegroups.com that he could create 100 milion
in 7 seconds on similar computer.
I need to speed this code somehow.
Could someone please look at it and help me to revise it?
Thanks a lot.
Sincerely,
Martin

Here is the code:

Private Const RAND_SEED As Integer = 123 ' Or anything
Private Const MAX_VALUE As Integer = 1000 ' This assumes single
digits

Private Sub Button1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button1.Click
TextBox1.Text = ""
Button1.Visible = False
Dim numero As String
Dim r As New Random(RAND_SEED)
For i As Integer = 1 To 50000
numero += r.Next(MAX_VALUE).ToString
' StatusBar1.Text = i
Next i
TextBox1.Text = numero
Button1.Visible = True
End Sub

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 20 '05 #2

P: n/a
Ok you gave me some idea how to speed up a process, and I was able to
push it from 5000/second to almost 1.000.000/second.
Now my code generates always the same sequence of random numbers in
this extra speed of 1.000.000 per second.
Which is unfortunatelly still less than Mersenne Code which is able to
do 100.000.000 in 7 seconds on pentium 4 (as per someone from science
groups)
Hm... could anyone come up with any other solution to speed this up?
Little mind bender :)

This is my code so far:
Private Const RAND_SEED As Integer = 1234 ' Or anything
Private Const MAX_VALUE As Integer = 10 ' This assumes single
digits
Private Sub Button1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button1.Click
TextBox1.Text = ""
Button1.Visible = False
Dim numero(10000000) As String
Dim r As New Random(RAND_SEED)
For i As Integer = 1 To 10000000
numero(i) = r.Next(MAX_VALUE).ToString
' StatusBar1.Text = i
Next i

TextBox1.Text = numero(1) + " + " +
numero(50000) + " + " + numero(10000000) '
proof that number on same positions are the same

Button1.Visible = True
End Sub

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 20 '05 #3

P: n/a
Yes, use unmanaged code, do it in C++, the web will be full of examples.

Regards - OHM
Martin Ho wrote:
Ok you gave me some idea how to speed up a process, and I was able to
push it from 5000/second to almost 1.000.000/second.
Now my code generates always the same sequence of random numbers in
this extra speed of 1.000.000 per second.
Which is unfortunatelly still less than Mersenne Code which is able to
do 100.000.000 in 7 seconds on pentium 4 (as per someone from science
groups)
Hm... could anyone come up with any other solution to speed this up?
Little mind bender :)

This is my code so far:
Private Const RAND_SEED As Integer = 1234 ' Or anything
Private Const MAX_VALUE As Integer = 10 ' This assumes single
digits
Private Sub Button1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button1.Click
TextBox1.Text = ""
Button1.Visible = False
Dim numero(10000000) As String
Dim r As New Random(RAND_SEED)
For i As Integer = 1 To 10000000
numero(i) = r.Next(MAX_VALUE).ToString
' StatusBar1.Text = i
Next i

TextBox1.Text = numero(1) + " + " +
numero(50000) + " + " + numero(10000000) '
proof that number on same positions are the same

Button1.Visible = True
End Sub

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com
Nov 20 '05 #4

P: n/a
Cor
Hi Martin,

Can you try this
\\\
Dim numero(10000000) As Double
Dim r As New Random(RAND_SEED)
For i As Integer = 1 To 10000000
numero(i) = (r.Next(MAX_VALUE))
Next
TextBox1.Text = numero(1).ToString & " + " _
& numero(50000).ToString & " + " & _
numero(10000000).ToString() '
///

I hope this works?

Cor
Dim numero(10000000) As String
Dim r As New Random(RAND_SEED)
For i As Integer = 1 To 10000000
numero(i) = r.Next(MAX_VALUE).ToString
' StatusBar1.Text = i
Next i

TextBox1.Text = numero(1) + " + " +
numero(50000) + " + " + numero(10000000) '
proof that number on same positions are the same

Nov 20 '05 #5

P: n/a
"Martin Ho" <ma******@74tech-dot-com.no-spam.invalid> schrieb
Dim numero(10000000) As String
Dim r As New Random(RAND_SEED)
For i As Integer = 1 To 10000000
numero(i) = r.Next(MAX_VALUE).ToString
' StatusBar1.Text = i
Next i


Does the result have to be a string? Why not declare numero as Integer? If
not all numbers will be used you could only convert those needed as a string
on demand. Or: declare numero as char and use
numero(i)=convert.tochar(r.next(max_value) + 48)
Might be a little bit faster than string.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6

P: n/a
Cor
100 times as byte

If you want the test I paste it in

Does the result have to be a string? Why not declare numero as Integer? If
not all numbers will be used you could only convert those needed as a string on demand. Or: declare numero as char and use
numero(i)=convert.tochar(r.next(max_value) + 48)
Might be a little bit faster than string.

Nov 20 '05 #7

P: n/a
I just woke up... I am going to try it soon...
Will get back to you guys,

thanks for trying to help me with it...

martin
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 20 '05 #8

P: n/a

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the
InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(88,
16)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(32,
80)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(248, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = "TextBox1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region
Private Const RAND_SEED As Integer = 1234 ' Or anything
Private Const MAX_VALUE As Integer = 10 ' This assumes single
digits

Private Sub Button1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button1.Click
Button1.Visible = False

Dim numero(10000000) As Double

Dim r As New Random(RAND_SEED)
For i As Integer = 1 To 10000000
numero(i) = (r.Next(MAX_VALUE))
Next

Button1.Visible = True
TextBox1.Text = numero(1).ToString & " +
" & numero(50000).ToString & " + "
& numero(10000000).ToString()
End Sub
End Class

This is the code I am using now, as per Cor suggestion and it produced
10 milion digits sequence in under 1 second... I am very thankful for
this code Cor.
Thanks a lot.
Martin Ho
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 20 '05 #9

P: n/a
On 13 Jan 2004 23:48:55 -0600, Martin Ho wrote:
Hm... could anyone come up with any other solution to speed this up?
Little mind bender :)

numero(i) = r.Next(MAX_VALUE).ToString


I think the String conversion is what is slowing you down. I used the
following (quick and dirty) code to generate 100 million random integers in
just over 8 seconds. Its a P4 3Ghz, though.

Private Sub Button1_Click(...) Handles Button2.Click
Dim oRand As New Random(Environment.TickCount)
Dim intArray(99999999) As Integer

Dim dStart As DateTime = Date.Now

For x As Integer = 0 To 99999999
intArray(x) = oRand.Next
Next

Dim tsSpan As TimeSpan = Date.Now.Subtract(dStart)

MsgBox(tsSpan.TotalMilliseconds.ToString)

End Sub

HTH

--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #10

P: n/a
Well Chris,
but your code is generating a sequence which will be always
different.
I need to have the same sequence every time I run it.
But thanks for suggestion.
Martin
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 20 '05 #11

P: n/a
On 14 Jan 2004 10:46:00 -0600, Martin Ho wrote:
I need to have the same sequence every time I run it.
But thanks for suggestion.


Yes, but if you use the same seed when creating the random object, you
should get the same sequence. Anyway, I was only addressing the speed
issue, but I see that Cor helped you.

--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #12

P: n/a
Yes, he did, thank you anyways for your interest and time you
spentwith it.
Martin

backup of this topic is at:
http://www.dotnetboards.com/viewtopic.php?t=10558
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.