|
 I let this run for an hour to get to 5000 before I gave up. Someone with more CPU and physical RAM than I have should run it and see where it ends...
using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom.Compiler;
namespace MethodCountLimitFinder {
class Program {
static void Main(string[] args) {
Int32 methodCount = 1;
Microsoft.CSharp.CSharpCodeProvider cscp = new Microsoft.CSharp.CSharpCodeProvider();
ICodeCompiler icc = cscp.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
CompilerResults cr = null;
string pre = "using System;" + Environment.NewLine +
Environment.NewLine +
"namespace Tester {" + Environment.NewLine +
" class Test {" + Environment.NewLine;
string post = " }" + Environment.NewLine +
"}";
string inner = string.Empty;
while (true) {
inner += " public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine +
" return 42;" + Environment.NewLine +
" }" + Environment.NewLine;
cr = icc.CompileAssemblyFromSource(cp, pre + inner + post);
if (cr.Errors.Count > 0)
break;
methodCount++;
if (methodCount % 10 == 0)
System.Console.WriteLine(methodCount.ToString());
}
System.Console.WriteLine(methodCount);
}
}
}
Share and enjoy
Sean
|
|
|
|
|
Sean Michael Murphy wrote: while (true) {
inner += " public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine +
" return 42;" + Environment.NewLine +
" }" + Environment.NewLine;
cr = icc.CompileAssemblyFromSource(cp, pre + inner + post);
if (cr.Errors.Count > 0)
break;
methodCount++;
if (methodCount % 10 == 0)
System.Console.WriteLine(methodCount.ToString());
}
Sean Michael Murphy wrote: Someone with more CPU and physical RAM than I have should run it and see where it ends...
No wonder, always use StringBuilder for string concatenation in a loop.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
Cannot been said to often!
Good answere!
|
|
|
|
|
Well it can be said too often, but it's appropriate here.
|
|
|
|
|
dnh wrote: No wonder, always use StringBuilder for string concatenation in a loop.
Hmmm. Interesting. When I originally undertook to code this snippet to try to figure an answer to this guys question, optimization was pretty far from my mind. I mean, I cranked the original bit of code out in 15 minutes (or so) and had originally coded it so the methods would be recreated every time. I took another 5 minutes and optimized it so that only 1 method (the new one) would have to be concatenated to the "guts", which was then stuck in between the fixed "header" and "footer" of the class.
It ran slowly, but I assumed that most of the overhead was in the actual code compilation (compiling classes of 15000 lines), and not a little bit of string concatenation. So I've re-written it using StringBuilder and timed both versions for 500 iterations. The original code did 500 iterations on my PC in 161.5222 seconds. This version:
StringBuilder inner = new StringBuilder();
DateTime startTime = DateTime.Now;
for (Int32 i = 0; i < 500; i++) {
inner.Append(" public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine +
" return 42;" + Environment.NewLine +
" }" + Environment.NewLine);
StringBuilder code = new StringBuilder(pre);
code.Append(inner);
code.Append(post);
cr = icc.CompileAssemblyFromSource(cp, code.ToString());
if (cr.Errors.Count > 0)
break;
methodCount++;
if (methodCount % 10 == 0)
System.Console.WriteLine(methodCount.ToString());
}
TimeSpan ts = DateTime.Now - startTime;
System.Console.WriteLine(ts.TotalSeconds); did it in 160.111. Much less that 1% slower.
Not a string concatenation to be found, except for the line joins.
Anything to add?
Thanks.
Sean
|
|
|
|
|
Sean Michael Murphy wrote: Anything to add?
I'd agree that most time takes compilation, but the thing about string concatenation with + is that it's -unlike compilation - completely unnecessary. And I don't think that using StringBuilder for concatenating strings in big loops is optimalization - I think it's something you should do without thinking.
btw you're still allocating 7 or so strings in
inner.Append(" public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine +
" return 42;" + Environment.NewLine +
" }" + Environment.NewLine); every cycle, that's 3500 unnecessary allocations
Anyway, cool way to check for number of methods limit indeed.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
dnh wrote: I think it's something you should do without thinking.
Never do anything without thinking.
|
|
|
|
|
I'll repeat: *Always* use string builder for concatenating strings in big loops. And I stay behind my claim.
That being said, if that loop had about 5 iterations in 99,99% and much more in 0,01%, then you have to thinkg about it - IIRC StringBuilder would be slower. But if that task is something that must end in some very limited time or under very limited memory, you can't afford that 0,01% and even if performing worse in average, StringBuilder would be better choice.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
Hi,
<br />
while( true)<br />
{<br />
do<br />
{<br />
inner.Append(" public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine + " return 42;" + Environment.NewLine + " }" + Environment.NewLine);<br />
methodCount++;<br />
} while ((methodCount % 1000) != 0); <br />
<br />
cr = icc.CompileAssemblyFromSource(cp, pre + inner.ToString() + post); <br />
<br />
if (cr.Errors.Count > 0) <br />
break; <br />
<br />
System.Console.WriteLine(methodCount.ToString() + " Compiled successfuly ==> so not succed");<br />
} <br />
System.Console.WriteLine(methodCount + " may be approximately to -1000 of method count");<br />
<br />
I modified slightly your code as the above and executed, Its going on till 100000 ( above 1 lakh ), My machine got down, So I planned to run today night.
Now I feeling, I shouldn't ask this question first of all
Thanks,
Vythees
-- modified at 5:24 Tuesday 3rd July, 2007
Thanks,
Vythees
|
|
|
|
|
vytheeswaran wrote: Now I feeling, I shouldn't ask this question first of all
Don't be crazy. I enjoyed thinking about it.
Sean
|
|
|
|
|
A) I don't think there's any need for including the NewLine s.
B) Why step by one? Why not double methodCount after each successful compile?
|
|
|
|
|
In reference to A: error CS1034: Compiler limit exceeded: Line cannot exceed 16777214 characters
The following succeeds at 1000000, and then I killed it while it tried 2000000.
namespace MethodCountLimitFinder
{
class Program
{
[System.STAThreadAttribute]
static void Main ( string [] args )
{
Microsoft.CSharp.CSharpCodeProvider provider =
new Microsoft.CSharp.CSharpCodeProvider() ;
System.CodeDom.Compiler.CompilerParameters cp =
new System.CodeDom.Compiler.CompilerParameters() ;
cp.GenerateExecutable = false ;
cp.GenerateInMemory = true ;
System.CodeDom.Compiler.CompilerResults cr = null ;
System.Text.StringBuilder inner =
new System.Text.StringBuilder ( "namespace Tester { class Test {" ) ;
int methodCount = 1000000 ;
while ( true )
{
System.Console.WriteLine ( methodCount ) ;
for ( int i = methodCount ; i > 0 ; i-- )
{
inner.AppendFormat ( "void M{0}(){{}}\n" , methodCount++ ) ;
}
inner.Append ( "}}" ) ;
cr = provider.CompileAssemblyFromSource ( cp , inner.ToString() ) ;
if ( cr.Errors.Count > 0 )
{
break ;
}
inner.Remove ( inner.Length - 2 , 2 ) ;
}
foreach ( System.CodeDom.Compiler.CompilerError ce in cr.Errors )
{
System.Console.WriteLine ( ce.ToString() ) ;
}
}
}
}
-- modified at 21:11 Tuesday 3rd July, 2007
2000000 and counting...
C:\>maxi
1000000
2000000
4000000
error CS0001: Internal compiler error (0x80004005)
error CS0001: Internal compiler error (0xc0000017)
error CS0583: Internal Compiler Error (0xc0000005 at address 5A16E208): likely culprit is 'PARSE'.
error CS0586: Internal Compiler Error: stage 'PARSE'
error CS0587: Internal Compiler Error: stage 'PARSE'
error CS0587: Internal Compiler Error: stage 'BEGIN'
C:\>
-- modified at 1:56 Wednesday 4th July, 2007
After 3000000 I started hitting resource limits and timeouts. So now I simply have a program write a file with the code and compile it at the command line, that got me to 4000000 so far...
|
|
|
|
|
PIEBALDconsult wrote: A) I don't think there's any need for including the NewLines.
B) Why step by one? Why not double methodCount after each successful compile?
Both excellent suggestions.
1) The NewLines was so I could preview the code during the initial stages of development. Same reason for the indents. I like even my autogenerated code to be neat and tidy.
2) Yup. Could have done a more efficient search, but was more interested in starting the app to get the result. By the time I had written the original and the slightly optimized version, I had spent 45 minutes and was getting tired of the exercise. And I thought that The Answer would actually be fairly low (thought it would probably be 256, 512 or 1024 max). I was surprised to see it climb over 2K, but kept expecting it to fail shortly.
It never did, so I published the snippet and the result and encouraged others to continue in the work.
The application was really intended as a starting point for figuring out the answer to this guys question. It was not a fully peer reviewed, optimized, documented, shrink-wrapped product, as you and others have adequately demonstrated by now...
Sean
|
|
|
|
|
Hi
I have written a code which will call a webservice.The name of the webservice is "XMLSportsIn" and have been configured in my local machine.Now when i executing the code which calls this webservice i am getting a error as below
{"Exception from HRESULT: 0x80072EE6"} System.Exception {System.Runtime.InteropServices.COMException}
i am not able to find why this error arises..i am just pasting my code which calls the webservice..
MSXML2.XMLHTTP40Class objXmlHTTP = new XMLHTTP40Class();
try
{
objXMLDocument.Load(objFI.FullName);
objXmlHTTP.open("POST", m_WebServiceName, null, null, null);
when i traced the code after the open method of objXmlHTTP, error arises.
Can any one please help me what to do to tackle this issue/
Advanced thanks
Regards
DilipRam
|
|
|
|
|
Why arent you using the normal way to implement a web service client in .NET?
|
|
|
|
|
You may need to check out InnerException property too.
|
|
|
|
|
Hi,
I have small but very annoying problem. My form contain tabcontrol with more tabpages. When I click on separate tabpage I want set focus to first textbox on selected tabpage. Second option for select tabpage is pressing keys from F1 to F8.
I tried set textbox as ActiveControl on this form and I tried set focus but it didn't work. I noticed in some cases when I press tab after selecting tabpage focus go to textbox. So I use SendKeys.Send("{Tab}"), but dont't work when I focus tabpage with pressing key.
I spent a lot of times to solve problem btu no success.
Has anybody solution for my problem?
Thanks in advance, Jure
|
|
|
|
|
Hi,
my first attempt would be to remove all fancy code (SendKeys, ActiveControl, ...)
and to set TabStop=true and TabIndex=0 for the control you want to get focus on a tab page.
If that is not sufficient, I would use the TabControl.SelectedIndexChanged event
Hope this helps.
|
|
|
|
|
Hi,
the use of Sendkeys or ActiveControl was just experiment from despair.
I already have set TabStop=true and TabIndex=0. When I run my code with debugger I can see than code where I set focus is called just before show new tabpage. But focus stay in textbox on previous tabpage.
Thanks anyway
|
|
|
|
|
Hi, again!
My problem is solved thanks to you. I move code from event TabControl.Selected to TabControl.SelectedIndexChanged as you have writen and now all work fine.
Thank you very much!
|
|
|
|
|
if (this.tabControl1.SelectedTab == this.tabPage1)
this.textBox1.Focus();
else if (this.tabControl1.SelectedTab == this.tabPage2)
this.textBox3.Focus();
That should do it.
|
|
|
|
|
Hi, I tried this again, but doesn't work. Focus stay in textbox on previous tabpage.
Thanks all the same.
|
|
|
|
|
When I obtain the content of the chip data, it shows the hex formatted display on the rich text format control...The entire content fits in the control well without the use of vertical scroll bar (which is enabled in the control properties)...
After obtaining the content of the new chip data, i used (rtfDispData.text = "") and the only first line clears up then display the new entire data (rich text control is refreshed)...
The interesting thing is that I have other chip that has more data in it and produced longer list of data, thus showing the vertical scroll bar. After dumping the big data again, the whole rich text control is cleared then display new data... (also used the same thing (rtfDispData.text = "")...
Why is this happening? seems that I have to use the vertical scroll bar to be seen in order to clear entire rtf control? I need to clear this control when the vertical scroll bar isn't visible. HELP!
|
|
|
|
|
Hi,
your story does not match my experience with RTB. Are you sure it does that ?
BTW There is a Clear() method that removes all text.
|
|
|
|
|
Weird eh?
Same issue for Clear() method.
again, weird eh?
|
|
|
|