|
The code shown translates to a few instructions only, so it takes a fraction of a microsecond to execute; if your text handling seems slow, look at the larger picture (and explain and show us more). Unless you need complex calculations and decisions, file input/output is likely to dominate the performance.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
I have decided to post the full source of the code so far, the code is a bit messy but I felt I had to break some coding standards to achieve higher performance so here it goes:
using System;
using System.Text;
using System.IO;
namespace CharacterReader
{
unsafe class Program
{
static readonly char[] Letter = new char[26];
static string _fileName = "lol.txt";
[STAThread]
static void Main()
{
using (System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog())
{
if (open.ShowDialog().Equals(System.Windows.Forms.DialogResult.OK))
{
_fileName = open.FileName;
}
}
uint *value = stackalloc uint[26];
Load();
Console.WriteLine("Reading File...");
ReadFile(_fileName);
Console.WriteLine("Analysing file...");
AnalyseFile(value);
Console.WriteLine("Done!");
GetTotal(value);
Console.ReadKey();
}
static int _index;
static int _i;
static void Load()
{
for (_i = 65; _i < 91; _i++)
{
Letter[_index] = (char)_i;
_index++;
}
_index = 0;
}
const int BufferSize = 512;
static readonly StringBuilder Contents = new StringBuilder(99999);
static private void ReadFile(string filename)
{
FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
StreamReader streamReader = new StreamReader(fileStream);
char[] fileContents = new char[BufferSize];
int* ptr = stackalloc int[1];
*ptr = streamReader.Read(fileContents, 0, BufferSize);
while (*ptr > 0)
{
Contents.Append(fileContents);
*ptr = streamReader.Read(fileContents, 0, BufferSize);
}
streamReader.Close();
fileStream.Close();
}
static void AnalyseFile(uint* value)
{
char* charPtr = stackalloc char[1];
for (_i = 0; _i < Contents.Length; _i++)
{
for (int k = 0; k < Letter.Length; k++)
{
*charPtr = Contents[_i];
if (*charPtr < 65 ) continue;
if (*charPtr >= 97 && *charPtr <= 122)
{
*charPtr = char.ToUpper(*charPtr);
}
if (!(*charPtr).Equals(Letter[k])) continue;
(value[k]) += 1;
break;
}
}
}
static void GetTotal(uint* value)
{
uint* biggestValue = stackalloc uint[1];
for (_i = 0; _i < 26; _i++)
{
if (*biggestValue >= value[_i]) continue;
*biggestValue = value[_i];
}
for (_i = 0; _i < 26; _i++)
{
if (!(*biggestValue).Equals(value[_i])) continue;
_index = _i;
break;
}
Console.WriteLine("The most common char is {0} with {1} hits", Letter[_index], *biggestValue);
}
}
}
Has anyone got some tips to make it run faster, it seems the "AnalyseFile" method seems to be slower than the file input
|
|
|
|
|
That looks horribly inefficient. Please replace CODE tags by PRE tags (so it gets formatted properly), then I will look into it.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Thanks :P
Here is an updated version of the code, it runs very fast which is suprising, the small project was actually quite a good learning experiance for me (being a student), one of the things I learnt was that higher performance in code seems to be achieved by decrementing coding standards
using System;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace CharacterReader
{
unsafe class Program
{
static readonly char[] Letter = new char[26];
static string _fileName = "lol.txt";
[STAThread]
static void Main()
{
using (System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog())
{
if (open.ShowDialog().Equals(System.Windows.Forms.DialogResult.OK))
{
_fileName = open.FileName;
}
}
Stopwatch stopWatch = new Stopwatch();
uint* value = stackalloc uint[26];
Load();
Console.WriteLine("Reading File...");
stopWatch.Start();
ReadFile(_fileName);
Console.WriteLine("Analysing file...");
AnalyseFile(value);
Console.WriteLine("Done!");
GetTotal(value);
stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed.TotalMilliseconds);
Console.ReadKey();
}
static int _index;
static int _i;
static void Load()
{
for (_i = 97; _i < 123; _i++)
{
Letter[_index] = (char)_i;
_index++;
}
_index = 0;
}
const int BufferSize = 512;
static readonly StringBuilder Contents = new StringBuilder(99999);
static private void ReadFile(string filename)
{
FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
StreamReader streamReader = new StreamReader(fileStream);
char[] fileContents = new char[BufferSize];
int* ptr = stackalloc int[1];
*ptr = streamReader.Read(fileContents, 0, BufferSize);
while (*ptr > 0)
{
Contents.Append(fileContents);
*ptr = streamReader.Read(fileContents, 0, BufferSize);
}
streamReader.Close();
fileStream.Close();
}
static void AnalyseFile(uint* value)
{
char* charPtr = stackalloc char[1];
for (_i = 0; _i < Contents.Length; _i++)
{
*charPtr = Contents[_i];
if ((*charPtr < 65) || (*charPtr > 90 && *charPtr < 97) || (*charPtr > 122)) continue;
*charPtr = char.ToLower(*charPtr);
value[*charPtr - 97] +=1;
}
}
static void GetTotal(uint* value)
{
uint* biggestValue = stackalloc uint[1];
for (_i = 0; _i < 26; _i++)
{
if (*biggestValue >= value[_i]) continue;
*biggestValue = value[_i];
}
for (_i = 0; _i < 26; _i++)
{
if (!(*biggestValue).Equals(value[_i])) continue;
_index = _i;
break;
}
Console.WriteLine("The most common char is {0} with {1} hits", Letter[_index], *biggestValue);
}
}
}
Thanks for your help :P
|
|
|
|
|
OK, PRE tags have turned that into a readable code snippet.
I see you got rid of the nested for loops; there was indeed no reason to have two nested loops.
Why are you still using that char pointer?
I would give the technique below a try:
static void AnalyseFile(uint* value) {
foreach(char c in Contents) value[(int)c]++;
foreach(char uc in "ABCDEFGHIJKLMNOPQRSTUVWXYZ") {
value[(int)uc.ToLower()]+=value[(int)uc];
}
comments:
1. performance optimization like these may be relevant only for large amounts of data
2. it has a different use of the value array
3. it will fail for non-ANSI text (when character values exceed 255).
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Ok everyone thanks for your help it was very helpful
Here is the final version of the code and I feel I have beaten it to death:
using System;
using System.Text;
using System.IO;
namespace CharacterReader
{
unsafe class Program
{
static readonly byte[] Letter = new byte[26];
static string _fileName;
[STAThread]
static void Main()
{
using (System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog())
{
if (open.ShowDialog().Equals(System.Windows.Forms.DialogResult.OK))
{
_fileName = open.FileName;
}
}
uint* value = stackalloc uint[26];
Load();
Console.WriteLine("Reading File...");
ReadFile( _fileName);
Console.WriteLine("Analysing file...");
AnalyseFile(ref value);
Console.WriteLine("Done!");
GetTotal(ref value);
Console.ReadKey();
}
static int _index;
static int _i;
static void Load()
{
for (byte i = 97; i < 123; i++)
{
Letter[_index] = i;
_index++;
}
_index = 0;
}
const int BufferSize = 512;
static readonly StringBuilder Contents = new StringBuilder(99999);
static private void ReadFile(string filename)
{
FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
StreamReader streamReader = new StreamReader(fileStream);
char[] fileContents = new char[BufferSize];
int* ptr = stackalloc int[1];
*ptr = streamReader.Read(fileContents, 0, BufferSize);
while (*ptr > 0)
{
Contents.Append(fileContents);
*ptr = streamReader.Read(fileContents, 0, BufferSize);
}
streamReader.Close();
fileStream.Close();
}
static void AnalyseFile(ref uint* value)
{
byte charPtr;
for (_i = 0; _i < Contents.Length; _i++)
{
charPtr = (byte)Contents[_i];
if (charPtr < 65) continue;
if (charPtr > 122) continue;
if (charPtr > 90 && charPtr < 97) continue;
if (charPtr < 97)
{
value[charPtr - 65] += 1;
}
else
{
value[charPtr - 97] += 1;
}
}
}
static void GetTotal(ref uint* value)
{
uint* biggestValue = stackalloc uint[1];
for (_i = 0; _i < 26; _i++)
{
if (*biggestValue >= value[_i]) continue;
*biggestValue = value[_i];
}
for (_i = 0; _i < 26; _i++)
{
if (!(*biggestValue).Equals(value[_i])) continue;
_index = _i;
break;
}
Console.WriteLine("The most common char is {0} with {1} hits", (char)Letter[_index], *biggestValue);
}
}
}
I read the entire English bible in under 6ms !
|
|
|
|
|
how to run a php code from your program?
like just execute an already existing php code from a remote server just like being a normal internet user requesting that the php is executed but without the need of any php-generated interface,just taking advantage of the server-side characteristics of php.
i wonder,can c# take some details that could be returned by php,maybe picking up info from cookies left on the host machine? or maybe some session details?
and another thing: can c# force a server to run a code generated right from the c# interface,maybe transferred through ftp?
thanks alot in advance!
|
|
|
|
|
Don't ask the same question twice. Continue this on the previous thread.
But, since you added some other questions:
andyxfun wrote: just like being a normal internet user requesting that the php is executed but without the need of any php-generated interface
You don't need a php-generated interface. The php script can be executed from a web page.
andyxfun wrote: ,just taking advantage of the server-side characteristics of php.
php doesn't know about, nor care about, if it's running on a server or a workstation. The machine it's running on looks the same as any other.
andyxfun wrote: i wonder,can c# take some details that could be returned by php,maybe picking up info from cookies left on the host machine?
Without a web server, there are no cookies.
andyxfun wrote: or maybe some session details?
No web server = no session.
andyxfun wrote: can c# force a server to run a code generated right from the c# interface,maybe transferred through ftp?
No. As I said in your previous thread, running code on a remote box is a VERY HIGH SECURITY RISK. This is not something you normally would do.
|
|
|
|
|
ok,yeah,altmost the same question but the details make the difference: in this one i take the idea of any general php script,with the possibility of adding some new scripts using your application.
the cookies and sessions thing i asked there was referring to the fact that when you visit a webpage, this can put some cookies and some session info on your computer's temp folder and i know for a fact that both php and javascript are able to read/write these cookies and i was curious if c# may be capable of the same thing,giving me a good medium for transferring data between the php files present on the server and my project from my local computer.
as you advised me in your other comment, i will try google-ing it and see what comes!
thanks a lot, you even raised my moral!
|
|
|
|
|
Hi
I want to know all Operating System's usernames who are working under a particular domain.
I need to bind it to Dropdown..
Thanks in Advance..
|
|
|
|
|
And?? I assuming this is Active Directory, correct? You have to provide a bit more detail than what you already have. What code have you tried? Where did you run into a problem? Any error messages?
|
|
|
|
|
hi!
please tell me, can i request the running of a .php file located on a server via a C# program and maybe return some info some way?
i am really good at php but still suck at c# and need to finish a project for school and this would save alot of time...
thanks a lot in advance!!!
|
|
|
|
|
It's possible, so long as the .php code you're running does not put up a user interface or require user interaction at all. Look into the WMI Win32_Process class and you'll find that you can launch remote processes, with some restrictions. Running code on a remote box is a HUGE security risk, so it's not going to be a simple little process to do.
|
|
|
|
|
it is all for a school project so security is not one of my problems...
anyway,i only need php to do some basic operations on serverside like connecting to databases, adding-retrieving information and so on,and i understand that i cannot use any user interface.
thanx for the info, awaiting some more exact instructions.
i am grateful,though!
|
|
|
|
|
andyxfun wrote: awaiting some more exact instructions
I gave you everything you needed to do your own research and have a working concept in one or two hours (assuming you know nothing of WMI).
All you need are the classes in the System.Management namespace (covered in the MSDN documentation), the WMI Win32_Process class (a little Google goes a long way here), and some curiosity, imagination, and time. Anything beyond that is spoon feeding you the answers, and I'm not going to do you that injustice.
|
|
|
|
|
ok
thanks a lot for your answer
right now i have a little more time to do this so i can start the search.
and yes,you are right, i know nothing of wmi...
ill get documented and try to solve all this.
thanks a lot again!
|
|
|
|
|
FYI: you can run PHP code on your local Windows machine, all it takes is a package such as XAMPP[^]. That is what I use to develop and test my web site.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
thanks but...unfortunately, this is not the case...
i am a web developer and have a lot more experience than i have in C#...
i already have an apache server with a mysql database and php installed on my local machine, along with other tools but this project is meant to be run from any machine like a normal program, without the need of additional software installed,other than .net framework...
i have the possibility to use the advantage of server-side scripting,as this does not need the actual host of the program to own any other software packages.
thanks for trying though!
|
|
|
|
|
andyxfun wrote: i am really good at php but still suck at c# and need to finish a project for school and this would save alot of time...
Would it help if you could run PHP on .NET[^]? The website has some very cool examples I are Troll
|
|
|
|
|
thanks for answering,gonna try that out
|
|
|
|
|
damn!...seems to be some kind of extension to .net or something like that...(not really enough time to eat the whole information...)
unfortunately,i can't use no extensions for the project itself to work...
gonna keep as a last resort,though...
|
|
|
|
|
|
Hi All,
where is the more fast metod to write a txt file?
Thanks a lot.Alex
|
|
|
|
|
This depends upon what you are doing, and where the text is coming from.
In general you'll probably need to look at TextWriter class, or one of the StreamWriter subclasses.Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
|
|
|
|
|
Here is link[^] with similar discussion.
|
|
|
|
|