|
First off, when you are timing, don't use DateTime.Now - use the Stopwatch class instead. DateTime is only accurate to +- 10ms according to MS, which is frankly pants. Stopwatch is accurate to the system hardware time tick
Secondly, Did you get the same number of matches?
Thirdly, given the difference is ~10%, that could well be within the limits of experimental error and they are both executing the same code! Try running it 6 or so times and see if you get the same results -= I bet you don't!
I just tried it myself (with Stopwatch), and I get a much more pronounced difference:
Contains : 507282
Regex : 882800
Contains : 535572
Regex : 876324
Contains : 491582
Regex : 876369
Contains : 464881
Regex : 884031
Contains : 529527
Regex : 877334
Contains : 545686
Regex : 878110
That's on a Dual Core, under Win7 64 bit, running .NET 4, for x86.
Switching to x64:
Contains : 560253
Regex : 933286
Contains : 587743
Regex : 869393
Contains : 550809
Regex : 862328
Contains : 581642
Regex : 871326
Contains : 578634
Regex : 878303
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Hi! I've changed the code and got these results
00:00:00.2780085
00:00:00.2824236
00:00:00.2805873
for Regex
00:00:00.3233195
00:00:00.3238453
00:00:00.3248080
for Contains
namespace RegexTest
{
public partial class Window1 : Window
{
List<string> _data;
public Window1()
{
InitializeComponent();
_data = new List<string>(50000);
Random rand = new Random();
byte[] data = new byte[4000];
for (int i = 0; i < 50000; i++)
{
rand.NextBytes(data);
_data.Add(Convert.ToBase64String(data));
}
}
private void buttonTestRegex_Click(object sender, RoutedEventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
{
Regex regex = new Regex("99");
int count = _data.Count;
int match = 0;
for (int i = 0; i < count; i++)
match += regex.IsMatch(_data[i]) ? 1 : 0;
}
sw.Stop();
this.textBoxRegex.Text = sw.Elapsed.ToString();
}
private void buttonTestString_Click(object sender, RoutedEventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
{
Regex regex = new Regex("99");
int count = _data.Count;
int match = 0;
for (int i = 0; i < count; i++)
match += _data[i].Contains("99") ? 1 : 0;
}
sw.Stop();
this.textBoxString.Text = sw.Elapsed.ToString();
}
}
}
I use x86 processor, .NET 3.5.
|
|
|
|
|
I just noticed that you're not doing anything with match , in a release build the whole for loop may be optimized out so you may not actually be doing any work.
How about this.textBoxString.Text = String.Format ( "{0} matches found in {1}" , match , sw.Elapsed ) ;
|
|
|
|
|
You are right, I've changed this to
this.textBoxRegex.Text = sw.Elapsed.ToString() + " " + match.ToString();
but the results didn't change much:
00:00:00.2823207 36117 for Regex
00:00:00.3306054 36117 for Contains.
|
|
|
|
|
I agree with Griff, and I raise you this...
While what you have may be suitable for your current needs, it may not generalize well. If a library function were doing Contains that way it would likely have the same signature as the existing Contains -- bool Contains ( string , string ) -- and it would have to create the Regex on each call. Your code as shown, knowing that it can use the same Regex instance, avoids this overhead. A more honest implementation may be even less efficient than the numbers Griff found.
|
|
|
|
|
OriginalGriff wrote: han a Regex if only because the Regex has to be parsed!
No. Compilation can be a one time cost and as such is not a factor in general for regex. However after that the regex must still be interpreted. And that cost does not go away.
|
|
|
|
|
Why would you even do this? Regexes are not fast, they are fairly slow, that's the price you pay for them being so flexible ... and when you want something simple that is already provided by a framework method, just use that.
|
|
|
|
|
Hi Experts,
Can anyone explain to me what is the graphicsDeviceservice in XNA? For what I know it serve the same function as CSS.
Can someone explain to me pls?
Thank you
|
|
|
|
|
Take a look here[^].
Programming is work, it isn't finger painting. Luc Pattyn
|
|
|
|
|
hi all , i want to sent latitude & longitude on click on map
to c# windows form textbox_latitude & textbox_longitude
this code send latitude & longitude on click on map but it send it to html input type="text" name="lat" ,input type="text" name="lng"
i want to transfer var lat , var lng to c# windows form textbox_latitude & textbox_longitude
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: Dragable and clickable marker with lat and lang in form</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(30.050077, 31.238594);
var myOptions = {
zoom: 7,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var infowindow = new google.maps.InfoWindow({
content: "Drag marker to position or click to place"
});
infowindow.open(map);
google.maps.event.addListener(map, 'click', function(event) {
var clickedLocation = new google.maps.LatLng(event.latLng);
var marker = new google.maps.Marker({
position: event.latLng,
title:"Marker position",
draggable: true,
map: map
});
infowindow.open(map,marker);
map.setCenter(event.latLng);
google.maps.event.addListener(map, 'click', function(event) {
marker.setMap(null);
});
google.maps.event.addListener(marker, 'dragend', function(event) {
var point = event.latLng;
var lat = point.lat();
var lng = point.lng();
document.getElementById("frmLat").value = lat;
document.getElementById("frmLon").value = lng;
});
var point = event.latLng;
var lat = point.lat();
var lng = point.lng();
document.getElementById("frmLat").value = lat;
document.getElementById("frmLon").value = lng;
});
}
</script>
</head>
<body onLoad="initialize()">
Lat: <input type="text" name="lat" id="frmLat" >
Lng: <input type="text" name="lng" id="frmLon" >
iW3B.info
</body>
</html>
thanks in advance
|
|
|
|
|
Are you using the WebBrowser control?
If so, add a script to the HTML document that returns the values of interest; then (from the Windows Form) call the WebBrowser.InvokeScript method with the name of the script and populate the form's controls / properties with the returned values.
|
|
|
|
|
|
|
I failed to mention that you need to specify that the WinForm object is going to be used for the scripting from JavaScript. The following property should be updated from your WinForm class:
webBrowser1.ObjectForScripting = this; Sorry for this oversight.
|
|
|
|
|
hey all,
i am new to c#
what am i trying to do is
make a panel in form1 and free hand draw on that and it should automatically display on the panel in form2.
can i do that?
and can anyone post some examples i can use?
please thanks a lot
|
|
|
|
|
You should capture the drawing elements and their parameters into some data structures, and paint the drawing from that data; if you want to see more than one instance, just paint as many as you like.
This article[^] could offer some useful guide lines.
|
|
|
|
|
thanks for the reply
i mean do you have any examples that i can work with
i am really really new to c#
|
|
|
|
|
Then go to a book store, look at a range of C# books, choose one or two that look interesting to you, buy them, and study them. Once you got the fundamentals, start reading some articles, a lot of good stuff can be found on this very site. Programming is work, it isn't finger painting.
|
|
|
|
|
Luc Pattyn wrote: Programming is work, it isn't finger painting. Killer sig material.
/ravi
|
|
|
|
|
0down votefavorite
I have a collection of NodeModel objects:
public class NodeModel : ViewModelBase
{
public Guid Id { get; set; }
public string Caption { get; set; }
public string Description { get; set; }
public NodeType Type { get; set; }
public List<NodeModel> Children { get; set; }
}
I need to provide Move Up and Move Down functions. To move an item up in the collection, it seems like I would need to know the the item's parent.
True is, I'm not really sure how to implement this. Anyone done this? Can you point me in the right direction?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
|
Mark, I don't think a linked list is a good basis for a model of a hierarchy.
best, Bill
"Humans are amphibians ... half spirit and half animal ... as spirits they belong to the eternal world, but as animals they inhabit time. This means that while their spirit can be directed to an eternal object, their bodies, passions, and imaginations are in continual change, for to be in time, means to change. Their nearest approach to constancy, therefore, is undulation: the repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis
|
|
|
|
|
Your objects seems akin to Controls on a WinForm, so why not use their model? Each Control has a ControlCollection holding its children, and a Parent property pointing to its parent (null for any topmost Control).
When in doubt, look for similar situations and reuse existing concepts.
|
|
|
|
|
|
Yes, I'm using WPF and ObservableCollections. The question then is, in a hierchical collection, how do you know a node's index position? Seems like I'll need to recurse.
If it's not broken, fix it until it is
|
|
|
|