473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
12 1346
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
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
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
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
"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
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
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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: hasadh | last post by:
Hi, is the assemly code for if..else and switch statements similar. I would like to know if switch also uses value comparison for each case internally or does it jump to the case directly at...
10
by: Henrik Andersen | last post by:
Hi newsgroup Does the c++ compiler generate faster code at execution time than the C# compiler? Does the c++ compiler have more optimisations than the C# compiler? Thanks in advance!
8
by: Scott Emick | last post by:
I am using the following to compute distances between two lat/long coordinates for a store locator - (VB .NET 2003) it seems to take a long time to iterate through like 100-150 locations -...
3
by: jesper | last post by:
I would like some feedback on this. A while back I was trying my hand at some pathfinding for a small game I was making. I did not know anything about it so I read some stuff and came up with the...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
10
by: Extremest | last post by:
I know there are ways to make this a lot faster. Any newsreader does this in seconds. I don't know how they do it and I am very new to c#. If anyone knows a faster way please let me know. All...
7
by: Wisgary | last post by:
I'm doing some benchmarking tests to compare Microsoft's CLR against Mono's CLR. I could use some suggestions for how to objectively compare the code. To my surprise the few tests I've run so far...
12
by: vunet.us | last post by:
Is there a suggestion I can make this code run faster: if(document.getElementById("1")){ doOne(); } if(document.getElementById("2")){ doTwo(); } .......................
23
by: Python Maniac | last post by:
I am new to Python however I would like some feedback from those who know more about Python than I do at this time. def scrambleLine(line): s = '' for c in line: s += chr(ord(c) | 0x80)...
3
by: Alpha83 | last post by:
Hi, Is there a code measuring tool that tells you which is more efficient cost-wise. For example, if I were to compare the following two identical code blocks, how do I know, which is more...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.