|
Hi Dave,
Excellent reply! Two more comments,
1.
For out parameter, like outParameter in your sample, even if it is a value type (struct), there is no additional copy, i.e. the instance of ExampleDateTime in function Now is the same as the one we pass into this function (variable example)?
2.
DaveyM69 wrote: there are two DateTimes, the nowDateTime and the one returned and copied from DateTime.Now.
Your above statement is really tricky to understand. Sorry my English is not good, could you express in some other words please?
regards,
George
|
|
|
|
|
Hi George,
Let me begin by saying I'm no expert in the internal workings of the framework or compiler! There are many people here who have a far more in depth knowledge of these things, and if I'm wrong, I'm sure they will point out my errors
As far as I know...
1. There is no additional copy as it works on the actual object passed as a parameter. If there is a copy (I'm not sure how to check), then maybe we could/should force it to be passed by reference?
2. DateTime.Now; returns an instance of the DateTime struct.
DateTime nowDateTime = ... obviously also creates an instance of the DateTime struct.
So now we have two DateTime instances. By DateTime nowDateTime = DateTime.Now; we're simply copying the value of the second to the first.
Obviously you control the lifetime of nowDateTime, I don't know the lifetime of DateTime.Now - I'm still learning too!
Dave
|
|
|
|
|
Hi Dave,
I am confused after reading your reply.
For 1, you mentioned there is no additional copy, for 2, you mentioned there is two copies DateTime.Now and nowDateTime, since we only need nowDateTime, I think the instance returned by DateTime.Now is an additional copy.
Anyway, seems your point 1 and point 2 are conflicting, could you clarify please?
regards,
George
|
|
|
|
|
No additional copy when using the example method with out parameter
Dave
|
|
|
|
|
Thanks Dave,
1. So you agree if we do not use out parameter, there is an additional copy?
2.
Even if we use out parameter, as I showed below, there is still an additonal copy.
- One copy in function Main when we create variable abcd;
- One copy in function Test2, when we create an instance representing DateTime.Now.
Any comments?
static void Test2(out DateTime abcd)
{
abcd = DateTime.Now;
}
static void Main(string[] args)
{
DateTime abcd;
Test2(abcd);
return;
}
regards,
George
|
|
|
|
|
DaveyM69 wrote: In DateTime nowDateTime = DateTime.Now; there are two DateTimes, the nowDateTime and the one returned and copied from DateTime.Now.
Well, actually there isn't.
The variable nowDateTime is just memory that is allocated to hold a DateTime value, but it doesn't contain a DateTime value before the return value from the Now method has been assigned to it.
The value of a local variable is undefined before it has been assigned, and if you try to read the value before it has been assigned, you get a compilation error.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks Guffa,
I think the process should be, when we access the static member of DateTime.Now, one of more instance of DateTime is created internally, then another copy is made for the return value, correct?
regards,
George
|
|
|
|
|
Hi Dave,
I do not quite understand -- "The other way would be for the Now property getter to be a method and require an out parameter. This would make things really complicated if you needed to do a simple calculation and didn't need a variable for the Now value." -- could you show some pseudo code please?
regards,
George
|
|
|
|
|
1) My guess is that DateTime.Now simply calls the win32 systemtime function, creates a new DateTime object preset to the system time, and returns it. So the answer is NO, there is no additional object created.
2) You're over-analyzing and wasting time.
BTW, the DateTime class is most definitely a class. It just so happens to have a few static properties.
(And a struct *is* a class.)
"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
|
|
|
|
|
Hi John,
I agree with you that the DateTime internally created an instance of DateTime, which represents the current time. But since DateTime is a struct, which is value type, for assignment of value type, it is always an additional copy. Any comments?
regards,
George
|
|
|
|
|
Hello everyone,
Here is my code for an HttpService. My question is, if there is no coming client request, both (1) and (2) wil be blocked and when there is a coming request,
1. the the callback function provided as the 1st parameter of the BeginGetContext function will be called;
2. and also for the handle wait on (result.AsyncWaitHandle.WaitOne()) will be signalled.
Are there any sequences for (1) and (2)? i.e. for asynchronous call, signal is set before callback is called or vice versa?
public class TestHttpServer
{
private int _Port = 0;
private HttpListener _Server = new HttpListener();
private Service1 _manager;
public TestHttpServer (Service1 manager)
{
_manager = manager;
}
public int ListenPort
{
get
{
return _Port;
}
set
{
_Port = value;
}
}
public void StartListen()
{
try
{
IAsyncResult result;
_Server.Prefixes.Add(String.Format("http://+:{0}/", 9099));
_Server.Start();
while (true)
{
result = _Server.BeginGetContext(new AsyncCallback(this.HttpCallback), _Server);
result.AsyncWaitHandle.WaitOne();
}
}
catch (Exception ex)
{
throw ex;
}
}
public void Stop(bool isTerminate)
{
_Server.Stop();
}
private void HttpCallback(IAsyncResult result)
{
HttpListenerContext context = _Server.EndGetContext(result);
HandleRequest(context);
}
private void HandleRequest(HttpListenerContext context)
{
string matchUrl = context.Request.Url.AbsolutePath.Trim().ToLower();
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.Close();
}
}
thanks in advance,
George
|
|
|
|
|
This is the old code what we have discussed. Are looking to write a HTTPServer ? If I am getting time, I will probably write a simple one and send you at the end of the day.
|
|
|
|
|
Yes, N a v a n e e t h! I have discussed similar code with you before and learned a lot from you. I feel asynchronous call is easy to write, but there are many things internal to discover so that we can use it more efficiently.
For this question, this is what I have further studied about the issue of dependency/sequence of callback and signal set of wait handle.
Let me know if you have got any further ideas?
regards,
George
|
|
|
|
|
This might sound like a silly question, but will it?
|
|
|
|
|
erm.... no
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Yes; the guy in the next cubicle may come smash it.
|
|
|
|
|
|
Eventually yes, but the other components will have already failed by that point so it's irrelevant.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
It will if you sit near me!
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Hey guys got a problem here
I create a datatable manually and bind in to the gridview. It can be done
But here the issue how to create a hyperlink column manually in datatable
and a button column in datatable
any advise or guide
i will seriously deeply apperciate the help
Thanks a million Bros and Sis
KaKaShi HaTaKe
|
|
|
|
|
Hi All,
I've having some problems using a Binary Reader to find a chunk of format information in a WAV file. Basically everything is working fine with my reader using the standard data chunks, thje problem starts when I parse the file for the actual "data" chunk. The WAV standard is a pretty loose file format and allows for all manner of chunks inbetween the basic information and where the actual WAVE data begins. Each chunk has an ID which is either "RIFF", "fmt ", other random ones, then there is a "data" chunk located at a variable location. The code below is shocking, I know hence why i'm here, it works for some files but its also horribly inefficient and is in no way reliable. Can someone enlighten me as to a sure fire way of getting to this "data" chunk?
private void LocateDataChunk(BinaryReader bReader)
{
int _pos = 0
for (int i = _pos; i < (int)riffChunk.dwFileLength; i++)
{
bReader.BaseStream.Position = i;
try
{
dataChunk.sChunkID = new string(bReader.ReadChars(4));
if (dataChunk.sChunkID == "data")
{
dataChunk.dwChunkSize = bReader.ReadUInt32();
return;
}
}
catch (Exception ex)
{
}
}
}
riffChunk.dwFileLength is the uint byte count of the file.
Any help on this would be great.
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
I don't think the code you posted is that bad. You could improve it a little by keeping track of the previous characters so you don't need to reset the position. I did some unscientific tests using a 20mb MemoryStream with the data part at the end.
Original 1x
Keep track of previous characters using an array like a circular queue. ~1.8x speedup
Same as above but treating characters as bytes. ~8x speedup to original
Read whole file into byte[]. ~20x speedup
long pos = 0;
byte[] previous = bReader.ReadBytes(4);
for (long i = bReader.BaseStream.Position; i < length; i++)
{
byte current = bReader.ReadByte();
previous[pos++ % 4] = current;
if (previous[pos % 4] == 'd' && previous[(pos + 1) % 4] == 'a' && previous[(pos + 2) % 4] == 't' && previous[(pos + 3) % 4] == 'a')
{
dataChunk.sChunkID = "data";
dataChunk.dwChunkSize = bReader.ReadUInt32();
return;
}
}
It would seem the text encoding has the biggest impact, I couldn't find many wav files to test so I don't know if my code behaves any better than yours does, of the ones I tested the actual speed impact was negligible maybe something else was causing the slowdown?
Check the files that aren't working in a hex editor (in VS Open File > Open Dropdown > Binary Editor) and make sure you take care between int/unit/long for positions, file lengths e.t.c and the endianess of what your reading.
Think you've already seen this article.
|
|
|
|
|
Thanks for this dave, i'll try your version later. The problem I'm having is that On some files it was taking an extraordinary amount of time to find the "data" string. I was kind of hoping there was some other way to quickly scan to this chunk and get the position rather than try{}catch{} at every position. Reading the file into memory isn't an option as some of the files could be 100mb+ wav files ...
Anyway, i'll try your version later and see how it performs. Really appreciate that. Thank you!
Cheers,
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Why on earth are you reading the file four characters at a time, looking for an identifier? The file is organised into chunks, where the four character identifier is followed by an int containing the size of the chunk. Just read the chunk size and use that to skip to the next chunk, instead of reading throught the chunk four characters at a time.
Besides, there is no guarantee that the chunk size is a multiple of four, so using that method you may just as well miss the next identifier.
Also, you are catching exceptions and just ignoring them, you should never do that. If you re-throw the exception, you will probably find that when the method takes long time to run, you have missed the data identifier so that you are reading the entire file, and then continue to try to read way past the end of the file, as your loop runs for four times the size of the file.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi Guffa,
This is the first time I've ever attempted dealing with files like this. I'm a C# newbie (as you can tell) your points about this are sound (scuse the pun) and make complete sense.
If you have the time is there any chance you could provide a code example? I'm going to go and have a play with this idea and see what I can do.
Much appreciated.
Cheers,
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|