|
|
Please don't post the same question twice.
How does a string have a max or min value, by what rules ?
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
You could create a user defined function in SQL Server 2005 to give Min and Max of a String, which you would have to define the rules of what is the Max of a string and Min of a string.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
hi all
i wount to select max(string)from database
the database is sql server2005
who i can do tht without do any thing in code(c#)
thanks for any body help me
Thaer
|
|
|
|
|
Don't post the same question twice, please
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
I've used C# for a few years now and have not needed to use its pointers feature. However, I'm in the process of optimizing some code, and I'm looking at pointers as one way to improve performance. The code I'm working with makes extensive use of arrays. Lots and lots of tight loops in which the results are assigned to elements in a array.
I wrote the following console app to compare pointer performance to array performance:
using System;
using System.Diagnostics;
namespace PointerTest
{
class Program
{
private const int Size = 10000000;
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
float[] data = new float[Size];
unsafe
{
fixed(float* p = data)
{
watch.Start();
for(int i = 0; i < Size; i++)
{
p[i] = 1.0f;
}
watch.Stop();
Console.WriteLine("Pointer: " + watch.Elapsed.ToString());
}
}
watch.Reset();
watch.Start();
for(int i = 0; i < Size; i++)
{
data[i] = 1.0f;
}
watch.Stop();
Console.WriteLine("Array: " + watch.Elapsed.ToString());
Console.Read();
}
}
}
The program assigns 10 million items to a pointer pointing to an array and then does the same thing with the array directly. Here are the results:
Pointer: 00:00:00.1147978
Array: 00:00:00.0993352
I ran the program several times, and while the times varied, though not by a great amount, the result was the same: the pointer method is slower.
However, when I used an array size below about 20,000, the pointer method beat out the array method by a small amount.
Any thoughts or insights?
|
|
|
|
|
That matches what I have found as well, as I mentioned in this article[^]. I don't know the reason for it, but I've always found it to be so. Perhaps it could have to do with some extra checks that might done for pointers that are omitted with arrays because the JIT can make assumptions about arrays that it can't about pointers. It would be interesting to find out for sure what the cause is - but atm I unfortunately don't have the time to go poking around in the generated machine code.
--Justin,
Microsoft MVP, C#
|
|
|
|
|
Leslie Sanford wrote: Any thoughts or insights?
I like open questions... they indicate wisdom: either awareness of ignorance,
or a truely open mind!
In my experience measuring performance by looking at elapsed time is very good
if some precautions have been taken:
1.
the time must be long enough to average out some statistical disturbances, typically
things the system may do in the background; that's disk accesses, Ethernet
packets, anti-virus, whatever. I would suggest to increase the job tenfold.
2.
Be careful with the special situation for the first experiment: if your tests
share something (such as an array), it will not have been allocated in physical
memory, it will not yet have been (partially) loaded in level3 or L2 cache, etc.
Quite often, when comparing two alternatives, the second wins, not because
it is faster, but just because it gets the better starting conditions.
So the minimum one should do is swap both to see how much of a difference that
gives.
My approach to cover both issues typically is to perform all N different tests
one after another, but then loop over it for say 100 times.
Then either ignore the first iteration entirely, and/or do some statistical
analysis on those N result arrays.
But even then, for N>2 the order of the tests may also be significant, so it
might be better to sequence them at random.
Hope this helps.
|
|
|
|
|
Luc Pattyn wrote: I like open questions... they indicate wisdom: either awareness of ignorance,
or a truely open mind!
Thank you!
Luc Pattyn wrote: Quite often, when comparing two alternatives, the second wins, not because
it is faster, but just because it gets the better starting conditions.
So the minimum one should do is swap both to see how much of a difference that
gives.
You are right! I changed the order of the test and got nearly opposite results. The pointer method beat out the array method repeatedly.
Luc Pattyn wrote: My approach to cover both issues typically is to perform all N different tests
one after another, but then loop over it for say 100 times.
Then either ignore the first iteration entirely, and/or do some statistical
analysis on those N result arrays.
But even then, for N>2 the order of the tests may also be significant, so it
might be better to sequence them at random.
I rewrote the test using a smaller array. Then I ran the tests in a loop 10000 times. The pointer method beat out the array method by a small amount. I changed the order of the tests within the loop and got the same results. I ran the app several times and got consistent results.
It would seem that the pointer method may be faster based on my tests. I'll have to decide if the possible gain in speed is enough to justify changing my code. And perhaps improve my tests by doing a more thorough analysis of the results.
Luc Pattyn wrote: Hope this helps.
Absolutely! It's been an eye opener. Thank you.
|
|
|
|
|
Of course you are getting the same result. You are using a pointer to an array, which will produce the exact same code as using a reference to an array.
Try using the pointer directly instead:
for(int i = 0; i < Size; i++) {
p++ = 1.0f;
}
That should be a bit faster, as it doesn't produce any code to check the boundary of the array.
You should include the fixed structure inside the timing to make a fair comparison, as that is needed for the pointer operation. You should also test different sizes, to see how the code scales.
---
single minded; short sighted; long gone;
|
|
|
|
|
Guffa wrote: p++ = 1.0f;
Unfortunately, that doesn't compile. I have to do this:
fixed(float* pSource = data)
{
float* p = pSource;
for(int i = 0; i < Size; i++)
{
*p = 1.0f;
p++;
}
}
Guffa wrote: That should be a bit faster, as it doesn't produce any code to check the boundary of the array.
My tests show that the above is slower than using the subscript operator on the pointer; the array method beats it out. I assume it's because incrementing the pointer is taking additional time.
|
|
|
|
|
I think you're getting messed up by memory management. If I just reverse the order of the tests I get opposite results. Try setting the data size to something around 1000 and then put an additional loop from 1...1000000 around the each of the for( int... Size) loops.
The loops are still pretty much the same. One adds i * sizeof( float ) each time around, the other increments by sizeof( float ).
The optimizer seems pretty stupid however. Changing
*p = 1.0f;
p++;
to
*p++ = 1.0f
nearly halves the result. Perhaps there is some switch to improve that sort of thing.
-- modified at 13:05 Saturday 28th July, 2007
looked at your post again and now see you're asking about the memory size impact so I guess I'm just restating the obvious.
|
|
|
|
|
Robert Surtees wrote: If I just reverse the order of the tests I get opposite results.
That was my experience as well.
Robert Surtees wrote: The optimizer seems pretty stupid however. Changing
*p = 1.0f;
p++;
to
*p++ = 1.0f
nearly halves the result. Perhaps there is some switch to improve that sort of thing.
Wow. I changed to *p++ = 1.0f and noticed an appreciable difference between the pointer method and the array method.
I have to say though that...
*p++ = 1.0f;
...makes the old C programmer in me cringe. But if it's that much faster...
|
|
|
|
|
Robert Surtees wrote: The optimizer seems pretty stupid however. Changing
*p = 1.0f;
p++;
to
*p++ = 1.0f
nearly halves the result. Perhaps there is some switch to improve that sort of thing.
Are you compiling in release mode, and not debug mode?
In that case it's a bit surprising.
---
single minded; short sighted; long gone;
|
|
|
|
|
I'm pretty sure it was a release build but I'm a bit of a rookie at VS so I'm next to positive I did something wrong and running in debug would for sure mess up the results. I can't imagine why else it would be different.
|
|
|
|
|
Well, the resulting code is a bit different, even in release mode.
The C# code:
*p = 42;
p++; vs.
*p++ = 42;
The IL code:
ldloc.1
ldc.i4.s 0x2a
stind.i4
ldloc.1
ldc.i4.4
conv.i
add
stloc.1 vs.
ldloc.1
dup
ldc.i4.4
conv.i
add
stloc.1
ldc.i4.s 0x2a
stind.i4
The generated code:
mov dword ptr [ebx],2Ah
add ebx,4 vs.
mov edi,ebx
add ebx,4
mov dword ptr [edi],2Ah
I guess that the second code may be faster as the add and mov can be executed in parallell. In the first code the add has to wait for the move, as they use the same register.
---
single minded; short sighted; long gone;
|
|
|
|
|
Hi Leslie,
This is related to the formatting, not the topic, of your message.
I am preparing an article on copying code snippets from a CodeProject message board to
Visual Studio; seems there are no problems when the browser used is FireFox, Safari, ...
but often problems arise when using Internet Explorer.
One of the factors is the tags that are present inside the < PRE> </PRE> block.
In your message the lines are separated by <br> tags; most other messages have
just regular newlines.
Could you please tell me how you get that, i.e. what tools you use, and how you go
about it. Thanks in advance.
|
|
|
|
|
Luc Pattyn wrote: One of the factors is the tags that are present inside the < PRE> </PRE> block.
In your message the lines are separated by <br> tags; most other messages have
just regular newlines.
Could you please tell me how you get that, i.e. what tools you use, and how you go
about it. Thanks in advance.
The only thing I do differently is add a space to blank lines. I noticed in IE that if you simply copy code from VS and paste it into a message here at Code Project, blank lines are ignored. I pointed this out on the Suggestions message board, and someone showed me that by adding a space to the blank line, IE will added a break.
So instead of this:
fixed(float* pSource = data)
{
float* p = pSource;
for(int i = 0; i < Size; i++)
{
*p = 1.0f;
p++;
}
}
I add a space to the blank line to get this:
fixed(float* pSource = data)
{
float* p = pSource;
for(int i = 0; i < Size; i++)
{
*p = 1.0f;
p++;
}
}
[EDIT]As far as the tools I use, I just copy code out of Visual C# 2005 edition.[/EDIT]
|
|
|
|
|
Yep, but you walked around what I really wanted to know:
which browser do you use for posting CP messages that include code snippets ?
and do you add any tags inside the PRE tags, such as CODE, BR, other ?
TIA
|
|
|
|
|
Luc Pattyn wrote: Yep, but you walked around what I really wanted to know:
which browser do you use for posting CP messages that include code snippets ?
and do you add any tags inside the PRE tags, such as CODE, BR, other ?
I use IE7. I don't add any tags inside the PRE tag.
|
|
|
|
|
OK, thanks. So I still need to figure out why your snippets are harder to copy
wiht formatting than most others... (that's with IE6/IE7 towards Visual Studio).
|
|
|
|
|
Luc Pattyn wrote: So I still need to figure out why your snippets are harder to copy
wiht formatting than most others... (that's with IE6/IE7 towards Visual Studio).
I'm posting this from Firefox. Let me know if the code below has any additional tags added.
fixed(float* pSource = data)
{
float* p = pSource;
pointerWatch.Start();
for(int i = 0; i < Size; i++)
{
p[i] = 1.0f;
}
pointerWatch.Stop();
}
I'm posting this on the off chance that something in my preferences in Visual C# Express could be embedding html tags.
|
|
|
|
|
Hi Leslie,
there is not really a difference
for both of them, and using IE6 (or IE7):
1. when selecting some code, copy, paste, line breaks are gone
2. when selecting some code, copy, apply my tool, paste, line breaks are OK
but indents are gone
3. when selecting some code up to the closing PRE tag, copy, apply my tool,
paste, everything is fine
As such it is worse than most posts, since they do 2 and 3 fine.
All other browsers we have tested (Netscape, FireFox, Safari, Maxthon) show
no problem whatsoever in this regard.
I am still puzzled what the essential difference is between a small minority
of posts (including yours) and all the others. So I was hoping these few all did
the same unknown something while publishing. So far I dont know. Seems not.
Since each CodeProject page is a lot of HTML, it is really hard to tackle.
You will be able to read more on this next week, when the article will be published.
Regards.
|
|
|
|
|
Hi all
My combobox is html control I fill it from html control but I like to Conective his Items With my code from javascript What I can do
if you hava asmples give me it please
thanks evry body help me
Thaer
|
|
|
|
|
If your control has an id, you can use document.getElementFromId to get the control in javascript. What do you want to do with it ?
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|