Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
VFP vs C# String handling
Message
From
22/05/2011 10:38:35
 
 
To
22/05/2011 09:38:19
General information
Forum:
Visual FoxPro
Category:
Other
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Miscellaneous
Thread ID:
01511379
Message ID:
01511397
Views:
95
>Why don't you run the same VFP test Steve did. http://www.stevenblack.com/text.html

Just duplicated one of Steve's tests, since he added "Locating sub-strings in strings is something VFP does really well."

VFP :
CD c:\users\pagan\documents\test
fname = "war and peace.txt"

SET SAFETY OFF

nStart = SECONDS()
LOCAL t1, t2, x, i
LOCAL ARRAY aStrings[5]
x= FILETOSTR(fname)
aStrings[1]= "Russia"  && 775 occurences
aStrings[2]= "Anna"    && 293 occurences
aStrings[3]= "Czar"    && 4 occurences
aStrings[4]= "windows" && 23 occurences
aStrings[5]= "Pentium" && 0 occurences

*x= FILETOSTR("WarAndPeace.TXT")   && 3.2 mb in size
FOR i = 1 TO ALEN( aStrings)
  t1= SECONDS()
OCCURS(aStrings[i],x)
  t2= SECONDS()
[OCCURS( "]+aStrings[i]+[", x)], t2-t1, "seconds"  && 0.401 seconds avg
ENDFOR

Times ranged from : Pentium 0 occurs - .147 seconds to Russia 775 occurs .185 seconds

Here's C#
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();


            string fname = @"C:\Users\Pagan\Documents\test\war and peace.txt";
            string foo = File.ReadAllText(fname);

            countem(stopwatch, foo, "Russia");
            countem(stopwatch, foo, "Anna");
            countem(stopwatch, foo, "Czar");
            countem(stopwatch, foo, "windows");
            countem(stopwatch, foo, "Pentium");

            Console.Read();
        }

        private static void countem(Stopwatch stopwatch, string foo, string pattern)
        {

            Regex r = new Regex(pattern, RegexOptions.None);
            stopwatch.Start();
            MatchCollection matches = r.Matches(foo);
            stopwatch.Stop();
            Console.WriteLine("Word :{0} - Count : {1} - {2} Seconds", pattern, matches.Count.ToString(), stopwatch.Elapsed);
        }
    }
Ranges from Russia - 775 occurs - .0000065 seconds to Pentium - 0 occurs - .0000130 seconds

( interesting that the fastest was the most finds and 0 was the slowest - Anna - 293 - .00000082 seconds etc )

>
>>I remember about 10 years ago Steve Black giving a presentation at VFE Devcon in Las Vegas about string handling in VFP, using War and Peace from Project Gutenberg and I remember all of us going "Oooooh". Since that time I have taken it as an article of faith that VFP had string handling powers beyond the reach of other languages.
>>
>>So in light of my surprising results on the loop in VFP vs Other Languages(Python/Ruby) I thought I'd try something similar.
>>
>>First, the task was to load War and Peace ( over 3.2 million characters ) into a string and replace war with PEACE and peace with WAR. To give VFP the edge, I made the finding case insensitive ( for some perverse reason C# does not have a case insensitive string replace )
>>
>>Here's the VFP code : ( my VFP is rusty so if there is a better way to do this, I'm listening )
>>
>>
>>CD c:\users\pagan\documents\test
>>fname = "war and peace.txt"
>>
>>SET SAFETY OFF
>>
>>nStart = SECONDS()
>>
>>x = FILETOSTR(fname)
>>
>>x = STRTRAN(x,"war","www",-1,-1,1)
>>x = STRTRAN(x,"peace","ppp",-1,-1,1)
>>x = STRTRAN(x,"www","PEACE")
>>x = STRTRAN(x,"ppp","WAR")
>>
>>=STRTOFILE(x,"peace and war.txt",0)
>>
>>nStop = SECONDS()
>>
>>? nstop - nstart
>>
>>
>>
>>On my box this takes about 2.3 seconds. Pretty cool.
>>
>>Here's my C# - compensating as best I can for the case sensitivity :
>>
>>
>>        static void Main(string[] args)
>>        {
>>            Stopwatch stopwatch = new Stopwatch();
>>            stopwatch.Start();
>>
>>            string fname =@"C:\Users\Pagan\Documents\test\war and peace.txt";
>>            string result = @"C:\Users\Pagan\Documents\test\csharp result.txt";
>>            string foo = File.ReadAllText(fname);
>>            StringBuilder sb = new StringBuilder(foo);
>>
>>            sb.Replace("war","www");
>>            sb.Replace("War","www");
>>            sb.Replace("WAR", "www");
>>            sb.Replace("peace","ppp");
>>            sb.Replace("Peace","ppp");
>>            sb.Replace("PEACE", "ppp");
>>            sb.Replace("www","PEACE");
>>            sb.Replace("ppp","WAR");
>>
>>            foo = sb.ToString();
>>            File.WriteAllText(result, foo);
>>
>>            stopwatch.Stop();
>>
>>            Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
>>            Console.Read();
>>
>>
>>six tenths of a second.
>>
>>It gets even more interesting when a loop is added to build the string to over 32 million characters by concatenating the original string ten times before doing the replacements, resulting in a 35 mb or so output file.
>>
>>C# - about 6 seconds
>>VFP just dies, shows as not responding in the task manager and has to be killed.
>>
>>It should be mentioned that the 10x test may not be entirely fair as the VS is 64 bit and the VFP is 32 bit and there are 6gb of ram on this box. But the single file version seems dispositive in any case.
>>
>>I'd encourage others to try this and tell me what I'm doing wrong.


Charles Hankey

Though a good deal is too strange to be believed, nothing is too strange to have happened.
- Thomas Hardy

Half the harm that is done in this world is due to people who want to feel important. They don't mean to do harm-- but the harm does not interest them. Or they do not see it, or they justify it because they are absorbed in the endless struggle to think well of themselves.

-- T. S. Eliot
Democracy is two wolves and a sheep voting on what to have for lunch.
Liberty is a well-armed sheep contesting the vote.
- Ben Franklin

Pardon him, Theodotus. He is a barbarian, and thinks that the customs of his tribe and island are the laws of nature.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform