Question Program to make a list of every character possibilty

Haxaro

Well-known member
Joined
Mar 13, 2009
Messages
105
Programming Experience
1-3
I have made a program that makes and saves every possibility of characters under 7 characters (so far), and it takes about 10 mins to make one million possibilities... this is about 1800 possibilities per second (differering on different CPU's and RAM's)

How can i make it more efficient, because i have made some basic calculations, and if i wanted 7 digits of every possible character combination, it will take about 7 billion possibilities... If i had 10 mins per million, that is a long time.. any more efficient methods???

(See attached source)
 

Attachments

Last edited by a moderator:

anthony.selby

Well-known member
Joined
Sep 3, 2009
Messages
65
Programming Experience
5-10
yes,

I would use a thread ... and if you do this on a multicore machine you could use multiple threads (using the new parallel namespace ... you would have to download it)

but I would also use a for loop

for count = 32 to 126
current = chr(count)
next


and when you get above 1 character you should use the stringbuilder class
 

Haxaro

Well-known member
Joined
Mar 13, 2009
Messages
105
Programming Experience
1-3
Thanks for your reply,
So does a for loop does the count for you; i.e. - i do not need to do the i = i + 1..

And what is the string builder class?

Finally, if i was to implement the above changes, how much faster could it become?

thanks :)
 

anthony.selby

Well-known member
Joined
Sep 3, 2009
Messages
65
Programming Experience
5-10
string builder is a class that lets you append text to a string ... its much faster then adding the string appending it to a normal string ... i don't know exactly how much faster you can make this ... but you could probably double the speed atleast ...
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
A string variable allocates space in memory the same as any other declared variables. The odd part about string variable though, when you concetanate a string, it doesnt actually update the original allocated variable, it instead creates a new allocated space in memory.

A string builder class is much faster as stated above and more efficient on memory. Concatenation in a string builder line is still slower then if you create a seperate append in your string builder too. Its seems odd to write so many append lines for long strings but it is much faster.

As to the speed of your program I would say there are many spots to improve upon besides this point. The use of timer events going off constantly is another thing slowing your program.
 

Haxaro

Well-known member
Joined
Mar 13, 2009
Messages
105
Programming Experience
1-3
So how do i call upon this string builder class???

What are some ways i can make it faster?

thanks :)
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
This is a sample directly from the help file but it isnt actually the most efficient use of it...

Code:
Private Function StringBuilderTest() As String

    Dim builder As New System.Text.StringBuilder

    For i As Integer = 1 To 1000
        builder.Append("Step " & i & vbCrLf)
    Next

    Return builder.ToString

End Function
First of all you can see that it still uses concatenation in each line it creates. It actually runs faster creating a seperate .append instead of contenating.

Code:
Private Function StringBuilderTest() As String

    Dim builder As New System.Text.StringBuilder

    For i As Integer = 1 To 1000
        'builder.Append("Step " & i & vbCrLf)
        
        With builder
            .Append("Step ")
            .Append(i)
            .Append(vbCrlF)
        End With
    Next i

    Return builder.ToString

End Function
Also this is an alternative rather then manually entering line breaks, you can instead use builder.AppendLine which automatically adds a line break.

Code:
Private Function StringBuilderTest() As String

    Dim builder As New System.Text.StringBuilder

    For i As Integer = 1 To 1000
        builder.AppendLine(String.Format("Step {0}", i))
    Next

    Return builder.ToString

End Function
However I have not actually tested the .AppendLine statements in speed tests as I have done with the former to state if it is more efficient ot not, specially considering I used the string.format method for this example.
 

Haxaro

Well-known member
Joined
Mar 13, 2009
Messages
105
Programming Experience
1-3
I just did some calculations, and even though its going at 1800 combonations per second, it will still take 121 mileniums or something.. but thanks for the help anywayz :)
 

Robert_Zenz

Well-known member
Joined
Jun 3, 2008
Messages
503
Location
Vienna, Austria
Programming Experience
3-5
I wouldn't do such heavy calculations in .NET/Mono or any other High-Level Framework. You should write this is in some low-level language, Vala, C or C++ or something similar, maybe even utilizing the CUDA Framework from Nvidia may help.

If you want to do this in VB.NET, I'd at least suggest to turn explicit Typecasting on (Option Strict), implicit typecasting can cost a lot of performance. Also I have to admit that I don't understand your code, though I'd just use some nested loops or recursion to go through all possibilities. If I got you right the output should look something like:

Code:
AAAAAAA
AAAAAAB
AAAAAAC
AAAAAAD
...
AAAABZY
AAAABZZ
AAAACAA
AAAACAB
Such algorithm should cover all possibilities...I've tried something similar once in C...I'll have a look at home if I still got the sources, maybe they'll help you.

Bobby
 
Top Bottom