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

M

Martin Ho

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
 
C

Chris Smith

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 said:
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
 
M

Martin Ho

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
 
O

One Handed Man [ OHM# ]

Yes, use unmanaged code, do it in C++, the web will be full of examples.

Regards - OHM


Martin said:
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
 
C

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
 
A

Armin Zingler

Martin Ho said:
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.
 
M

Martin Ho

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
 
M

Martin Ho

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
 
C

Chris Dunaway

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
 
M

Martin Ho

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
 
C

Chris Dunaway

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top