|
Feel up for a challenge?
This isn't homework (Don't worry :p)
Was doing some coding optimisations, when I thought up the following challenge:
Code:
static void Main(string[] args)
{
Stopwatch sw1 = new Stopwatch();
sw1.Start();
string shortString = "1234567890";
string largeString = "";
for (int j = 0; j < 50000; j++)
{
largeString += shortString;
}
if (largeString.Length == 500000)
{
Console.WriteLine("Pass 1");
}
else
{
Console.WriteLine("Fail 1");
}
if (sw1.ElapsedMilliseconds < 10000)
{
Console.WriteLine("Pass 2");
}
else
{
Console.WriteLine("Fail 2 - " + sw1.ElapsedMilliseconds);
}
Console.ReadLine();
}
Challenge:
Change ANY 2 lines of code ABOVE the
code line to display ONLY the following output
Pass 1
Pass 2
As it stands, this code snippet displays (On my PC):
Pass 1
Fail 2 - 38614
Hint: The speed of your computer (Should Be) Irrelevant...
Optimised, it took 211ms on my PC (Which is ALOT less than 10000)
Good Luck
-= Reelix =-
|
|
|
|
|
Any two lines above "// Touch Nothing Below Here"?
Easy ... only need to change one :P Move "sw1.Start();" to below the for loop
EDIT: And oh btw, your not calling stop on the stopwatch?
modified on Tuesday, May 20, 2008 11:44 AM
|
|
|
|
|
originSH wrote: Easy ... only need to change one Move "sw1.Start();" to below the for loop
Or, how about replacing the declaration of largeString...
string largeString = new string(' ', 500000);
...and then commenting out the concatenation in the loop.
It runs in 0.0006 seconds.
ssclaire
|
|
|
|
|
Or funny stuff like:
string largeString = new string(' ', 499990);
for (int j = 0; j < 1; j++)
and keep the inner loop untouched
modified 12-Sep-18 21:01pm.
|
|
|
|
|
The largeString variable doesn't even have to be a string, it only has to have a property named Length that returns the value 500000.
sw1.Start();
var largeString = new { Length = 500000 };
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
ROFL!
Never thought of that one
Congrats on the Unique Idea!
Your program will still take an insanely long time to finish, but hey :p
-= Reelix =-
|
|
|
|
|
Change Largestring to string builder and Append instead of concatenating...
Stopwatch sw1 = new Stopwatch();
sw1.Start();
string shortString = "1234567890";
StringBuilder largeString = new StringBuilder();
for (int j = 0; j < 50000; j++) {
largeString.Append(shortString);
}
if (largeString.Length == 500000) {
Console.WriteLine( "Pass 1" );
} else {
Console.WriteLine( "Fail 1" );
}
if (sw1.ElapsedMilliseconds < 10000) {
Console.WriteLine( "Pass 2" );
} else {
Console.WriteLine( "Fail 2 - " + sw1.ElapsedMilliseconds );
}
Console.ReadLine();
Pass 1
Pass 2
Keep DotNetting!!
GeekFromIndia
|
|
|
|
|
Thats the main answer I was looking for
string -> StringBuilder
Concat -> Append
Congrats :P
-= Reelix =-
|
|
|
|
|
oh..btw..it took just 29 ms on my machine after the change
Keep DotNetting!!
GeekFromIndia
|
|
|
|
|
for (StringBuilder b = new StringBuilder(); b.Length <= 500000; b.Length += 500000)
{
largeString += b.ToString();
}
Or am I missing something here? Takes exactly 2ms on my machine, and only 2 lines changed
Doing the StringBuilder stuff in a loop doesn't make much sense, but this way I could modify just 2 lines of code.
Pass 1
Pass 2 (2ms)
regards
modified on Tuesday, May 20, 2008 12:21 PM
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Heh... :p
Should have said the Loop-Line had to stay the same
Impressive Solution
-= Reelix =-
|
|
|
|
|
That's too easy. The obvious solution is to use a StringBuilder.
The real challange is to do it without using a StringBuilder.
And let's also test that the contents of the string is the same as with the original code:
bool pass = true;
for (int i = 0; i < 500000; i += 10) if (largeString.Substring(i, 10) != "1234567890") pass = false;
if (pass) {
Console.WriteLine("Pass 3");
} else {
Console.WriteLine("Fail 3");
}
Are you up for that?
I've got code that does it in 8 ms.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
|
Reelix wrote: And the (possible) answer YOU were looking for is one from further up...
Move the
sw1.Start();
JUST above the
// Touch Nothing Below Here
No, I have the sw1.Start(); at the top. No cheating.
Let's do it like this:
Stopwatch sw1 = new Stopwatch();
sw1.Start();
string largeString;
... code to populate the string here
if (largeString.Length == 500000) {
Console.WriteLine("Pass 1");
} else {
Console.WriteLine("Fail 1");
}
if (sw1.ElapsedMilliseconds < 1000) {
Console.WriteLine("Pass 2");
} else {
Console.WriteLine("Fail 2 - " + sw1.ElapsedMilliseconds);
}
bool pass = true;
for (int i = 0; i < 500000; i += 10) if (largeString.Substring(i, 10) != "1234567890") pass = false;
if (pass) {
Console.WriteLine("Pass 3");
} else {
Console.WriteLine("Fail 3");
}
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
|
No takers?
Here's how to do it really fast without a StringBuilder:
string shortString = "1234567890";
string miniString = string.Empty;
for (int i = 0; i < 125; i++) miniString += shortString;
largeString = string.Empty;
for (int i = 0; i < 15; i++) largeString += miniString;
for (int i = 0; i < 5; i++) largeString += largeString;
This runs in 2 ms on my computer, just as fast as using a StringBuilder.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Reminds me of the not-so-urban legend about the Chinese farmer who saved the emperors life and asked only one grain of rice in the first square of a chess board, twice that in the next square and so on for all 64 squares.
He obviously got his head chopped off once the emperor realized where things were going...
Later,
--
Peter
|
|
|
|
|
Change the first two lines to this:
Console.Write("Pass 1\nPass 2\n");
return;
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
You stole my answer.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
The \n should be Environment.NewLine since it differs on different platforms.
Regards,
Gareth.
(FKA gareth111)
|
|
|
|
|
Oh puh-lease! Exactly how many "platforms are you going to run a .Net app on? Ummmm, I predict ONE.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
I DID think someone would try something like
Console.WriteLine("Pass1\nPass2");
But it would crash the program...
Didn't think about return;
-= Reelix =-
|
|
|
|
|
Hi,
i'm having a problem with binding radiobuttons to bool properties of my own build class.
My class has 3 bool properties which are bound each to one radiobutton the Checked property. The 3 buttons are on the same panel so only one can be checked.
But it seems that selecting one of the radiobuttons has no effect. When i select one and go to another field/textbox) it jumps to the original value.
All other bindings to textboxes and checkboxes are good.
i'm working with VS2005, Dotnet 2.0.
Does anyone has any idea what goes wrong ?
Thx
Kurt
|
|
|
|
|
Good Day,
I am using BinaryFormatter to write my own file. The Formatter takes a STRUCT that I constructed and convert it to a byte array that I can write as file. However, for some strange reason, when the FILE is too large (200MB+), I get an out of memory exception. Here's the code:
public static byte[] RawSerialize(object anything)
{
MemoryStream Stream = new MemoryStream();
BinaryFormatter BF = new BinaryFormatter();
BF.Serialize(Stream, anything);
byte[] Data = Stream.ToArray();
return Data;
}
Please note that I have 3GB ram on my system and when the OOM error was called, my memory usage was only 20%.
Is there any workaround for this?
Thanks!
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.
|
|
|
|
|
Instead of dealing with in-memory byte arrays and streams, why not utilize the hard drive as the backing store stream? This way you won't have to load 200+ MB into memory.
Tech, life, family, faith: Give me a visit.
From my latest post: "It's sobering to watch: the whole country stops for 2 minutes as a siren is blast to remember the 22,437 victims of terror and soldiers who died in defense of Israel..."
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|