|
Why are you using doubles to calculate prime numbers when primes are all integers?
If it's just so you can use the square root - there is no rule saying you can't cast that back to an int, and even better, it's possible to calculate the integer square root.
And you don't even need the square root (which is slow to calculate) anyway, you could compare the square of i with x (which is obviously equivalent to comparing i with the square root of x ) - better yet, you don't even need to do the multiplication (you can use repeated adding of odd numbers)
Also, after testing whether x is even (if ((x&1)==0) ) you never need to put an even number into the trial division ever again since if i divides x and i is even, 2 also divides x .
And you're already applying parallelisation? It's a good thing this is just a test..
|
|
|
|
|
There is more to it.
All primes except 2 and 3 are of the form multiple-of-6 plus or minus 1, so the two threads should be devoted to those two series, i.e. the increment needs to be 6, not 4 so multiples of 3 aren't even considered (I agree, they would fall out pretty quickly).
And then all that fancy enumerator stuff is costing cycles; its overhead well exceeds its functionality. A cleanly coded single thread can completely outpace what has been shown. I have a primes workbench where I experiment a lot, it currently locates all primes up to 10 million in 0.25 seconds; and up to 1 billion in 6.8 seconds.
Eventually I will write a little article on it!
|
|
|
|
|
Or you could use one of those fancy sieves.. but then you're changing the whole algorithm of course (cheat)
But yea write that article, you've been talking about for over half a year (IIRC)
|
|
|
|
|
I'm still researching, and making progress...
|
|
|
|
|
Fair enough, but it's the only article I'm really looking forward to reading
|
|
|
|
|
That is too bad, as I am planning some articles on other topics too...
|
|
|
|
|
|
I have a whole list of topics, all in various stages.
I plan to publish around ten articles on CP this year.
The one I like most myself is about the dynamics of ThreadPool. It will have some nice graphs!
And the biggest undertaking may be a series about P/Invoke.
|
|
|
|
|
That sounds pretty interesting as well.. ok.. maybe I'm looking forward to more than 1 article
|
|
|
|
|
Yeah, I just threw it together based on his code. It was by no means meant to be a tutorial on the most efficient way to do this. It just demonstrated a concept is all. Also, the performance numbers on that machine, well, suck, because it is a virtual running on a host that sucks at running VM's. I'm not allowed to put VS2010 on a real machine around here yet.
|
|
|
|
|
Fair enough. The concept and your code is interesting, it is just the example is a bit unfortunate.
I am looking forward to the official release of .NET 4.0 so I can investigate how it works, and how it compares to the manual approach.
|
|
|
|
|
I've been playing with the RC on that VM for about a week now. It's really cool to finally get to toy around with Tasks and Parallel. But, truthfully, the custom enumerator I threw together came from toying around with Enumberable.Range. It only uses integer and the results it returns can be no longer than integers. So, I tried throwing a custom Range method that supported Long's together and that's what I adapted to quickly come up with that example. I took his point to be that he was trying to figure out how the PTL worked. So...
I'm very interested in seeing an article on the Prime Sieve you've got. It would be interesting to see if it could be adapted to use or work with PTL.
|
|
|
|
|
It probably will be a few more months before my article on primes gets finished.
I can tell you right now the parallel stuff will not be useful for it, multi-threading hardly helps at all here. The first step in my analysis is proving calculations are so fast (assuming a sieve) the job is bandwidth limited, not compute bound. So I optimized it with banding (locality of reference, cache efficiency), all on a single thread. Only then I set out to figure a way to get extra cores involved, and there hardly is any. IIRC a second thread yields no more than a few percents of extra speed, taking care of some initialization, as running the sieve itself is an inherently sequential operation.
|
|
|
|
|
Hi,
i need to get only title, author and lenght of mp3 file, how i can do this??
please help me!
|
|
|
|
|
You need to read id3 tags from the file.
|
|
|
|
|
|
A little researc using this information[^] should speed you on your way. txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
|
Bangalore, March 11, 2010: Darwin’s reference to biological symbiosis involves two or more species that commit to a relationship and through their combined efforts and mutual collaborative interactions, both stand to gain success. In a similar vein, there can be no doubt that one of the hottest spaces in enterprise software today is collaboration. It’s no surprise collaboration is getting a lot of interest. The old processes for capturing, authoring and sharing information are stale and inefficient. As such, there is lot of room for achieving productivity improvements through improved user experience. This has been true for all software, but especially so in the enterprise software space where collaboration is essential for daily operation, where every ounce of productivity translates into big dollars.
When we refer to collaboration on a business activity, there are generally two buckets for these processes. The first are activities that would fit within the process of innovation. This might include creating new products, models or business processes. In other words, we are talking about the process of creation and innovation by teams. The second bucket includes activities around executing the business processes. Execution includes the hundreds of processes that run each day to operate the business. These are tactical in nature, and traditionally have key performance indicators (KPIs) measuring the success of a given process.
In this day and age, fast is synonymous with instantaneous. Increasingly, there is less time to dial up a fax machine or wait for a file to slowly attach to e-mail (then wait yet again during sending). This is especially true for entrepreneurs, whose small businesses are built on every advantageous edge that makes them competitive with larger corporations as well as one another. For small business, everything must be real-time – not just information anymore, but also changes. Small business needs collaborative tools that can keep up.
Enter web-based collaborative tools, center stage. These business implementations are the only means fast enough to match the high-speed demands of the Internet-age industry. They achieve instantaneous cooperation despite distance, cost, and other past inhibitive considerations. In support of a powerful storage hub, they allow you to securely store your information, make real-time changes to it, share it openly or discretely, and, most importantly, collaborate on it with family, friends, groups, departments, business teams, clients, vendors, or anyone.
Fast isn't the only measure, though. These tools must be easy, with user-friendly interfaces and intuitive commands. Complicated FTP programs are a thing of the past, and e-mail is too limited (and sometimes too flaky) to fully meet a company's diverse needs. Tools must be cheap. The idea is productivity, and expensive software or services prohibit that goal. Web-based tools fill the demands traditionally met by established application suites, but without their hefty price tags attached. Most of all, though, tools must be reliable. Web-based collaboration does not depend on one machine (PC or Mac) or one server, but reliably and securely functions outside of these constraints in the cloud, so to speak. The information your enterprise team works on in the office all day should still be accessible at your home computer (so you can continue work with them at night). And with the extensive controls built in to web-based tools, even the most sensitive materials remain completely confidential despite their availability within user-defined limitations. Simply put, all-in-all web-based tools make collaboration simple.
In the pre-computer age (before 1970), collaboration was a manual process. This was done mainly in face-to-face meetings and phone calls. The technologies used included calculators, blackboards, telephones, postal mail and pencil on paper. While computers might be part of the process, it was mainframe host oriented that offered no interaction by the participants. This was labor intensive with long cycle times for even simple tasks. In this era, teams needed to be in the same location, or endure significant travel.
When the Internet entered the picture (1980s), it provided a basis for the team to communicate, mainly via email. This allowed teams to collaborate while geographically separated, which further improved the efficiency of the process. Since collaboration allowed teams to extend beyond a single location, they could now include more talented people in other geographies. Other technology advances such as overnight mail, video conferencing, faxing and conference calling further improved the process.
Another emerging area of how web collaboration is impacting execution is through business intelligence networks. These are like Facebook or Twitter on steroids. An example of this would be collecting sales and market intelligence through an internal network – very similar to a social network. The key difference is that in these applications the participants are internal communities, and the network has intelligence. Information is assembled and analytics push back conclusions based on all the inputs. This allows the community to respond quickly to new conditions. In other words, the decision latency is reduced closer to the dream of “real time business.”
Clearly, web collaboration has enabled business execution to be better, cheaper and faster than in the past.
Saltmarch Media's annual Great Indian Developer Awards honors software products across 12 categories,based on their productivity, innovation excellence, universal usefulness, simplicity, functionality and most importantly on the ground feedback from India’s software developer ecosystem. In the Collaboration Tools Category, the final shortlist consists of Citrix’s popular GoToMeeting, Microsoft’s Office, WebEx WebOffice, Microsoft’s Office SharePoint server and Microsoft’s Exchange Server. Oracle’s Collaboration Suite was the first collaboration tool to win this award in 2008 followed by Adobe Acrobat Connect Professional in the 2009 chapter of the same awards. Voting is open from http://www.developersummit.com/gida3_llist.
If there is a particular development environment that you personally endorse to your colleagues or you evangelize about them at the first opportunity you get, here is your chance to vote for it (voting closes April 10 2010) and see it win this prestigious award. Visit the 2010 Great Indian Developer Awards website and cast your vote. It counts!
A Saltmarch Media Press Release
E: info@saltmarch.com
Ph: +91 80 4005 1000
|
|
|
|
|
A coworker and I are working on a logging system for unhandled exceptions in our code (screenshots, logging system information, detailed exception info, etc) using the Application.ThreadException handler (our main app is in WinForms). From the exception, I can get the MethodInfo for the method that threw the exception, and from that get the DeclaringType of the class, and from that get all the class member variables and their values. Only one problem...I need to pass an instance of the class to the FieldInfo.GetValue method, but I can't find a way to get access to that instance in the ThreadException event handler. I spent a few hours this evening going over the System.Reflection documentation and put toegether a small console app to test a few theories, but I haven't found anything for getting a reference to a class instance from a MethodInfo , MethodBody , or Module object (and yes, I used the AppDomain.UnhandledException event for the console test app instead of Application.ThreadException ). Knowing the values of the class variables when the exception occurs would be very useful for debugging. Any ideas?
I'm also trying to find a way to get the current values for the function parameters and local variables when the exception occurs. I know, Reflection won't help since these values are all on the stack at runtime, and Reflection works off of compile-time metadata, but there must be some way to get this information. I found this thread[^] that mentions using the Debugging API, but I didn't see anything in that documentation that looked like it would help either. Any suggestions on this one?
I'm using VS2005 SP1 on XP Pro SP3 and Win7 Pro.
Thanks,
DybsThe shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
|
|
|
|
|
There's no easy way built-in to do this, but..
As an alternative, there's a Data [^] property on the exception. Perhaps you could put the internal state of the object that's throwing the exception in there?I are Troll
|
|
|
|
|
Eddy,
Thanks for the reply. In my case that won't really work because I need this information in the UnhandledException handler, and by the time I get to this point (the first time I actually have an exception to look at), I no longer have access to the object that threw the exception. I can certainly use this where I can handle an exception more gracefully and still want to log it with more details, but my goal is to get as much info as I can for unhandled exceptions.
Thanks,
DybsThe shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
|
|
|
|
|
dybs wrote: my goal is to get as much info as I can for unhandled exceptions
Ditto, and you're right - if the information contained helps with the debugging-proces. There's no easy anwer to your question, once one says that it's technically possible one has to answer whether it's worth the effort.
dybs wrote: In my case that won't really work because I need this information in the UnhandledException handler, and by the time I get to this point (the first time I actually have an exception to look at), I no longer have access to the object that threw the exception.
I have created a small console-application to test with;
class Program
{
static Form myForm = new Form();
static Button myButton = new Button();
[STAThread]
public static void Main(string[] args)
{
myButton.Click += delegate
{
new MyTextCache().DoLoadStuff();
};
myForm.Controls.Add(myButton);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(logEx);
Application.Run(myForm);
}
static void logEx(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = (Exception)e.ExceptionObject;
Console.WriteLine("Exception: " + e.ExceptionObject.GetType().ToString());
if (ex.Data.Count > 0)
{
foreach(var item in ex.Data)
{
var entry = (System.Collections.DictionaryEntry)item;
Console.WriteLine("Parameter: " + entry.Value.ToString());
}
}
}
} Rather simple, just creates a form with a button and logs any exceptions on the console. The key here is looping the Data property. We know that we can fill it when we throw an exception ourselves. Now, imagine that there's an unexpected exception thrown by a .NET class in the MyTextCache.DoLoadStuff method. To add the info, we'd need to catch it, attach the desired information, and rethrow it.
The MyTextCache could be adapted like shown below. There's a local exception-handler that doesn't handle the FileNotFound-exception, but instead adds a reference to the object;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
[Serializable()]
class MyTextCache: ISerializable
{
#region variables that represent Internal State
string _filename;
string _contents;
#endregion
public MyTextCache()
{
_filename = "Q:\\Hello World.txt";
}
public MyTextCache(SerializationInfo info, StreamingContext ctxt)
{
_filename = (String)info.GetValue("filename", typeof(string));
_contents = (String)info.GetValue("contents", typeof(string));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("filename", _filename);
info.AddValue("contents", _contents);
}
public override string ToString()
{
return string.Format("Filename: {0}\nContents: {1}\n",
_filename,
_contents);
}
internal void DoLoadStuff()
{
try
{
_contents = System.IO.File.ReadAllText(_filename);
}
catch(Exception e)
{
e.Data.Add("the object: ", this);
throw e;
}
}
} That would give you access to the _filename and _contents properties of the MyTextCache class. You should see an invalid filename, and an empty contents-property on the console if you attempt this.
There's a second problem; you can log the contents of the class now (or serialize it), but that doesn't give you access to the local variables from the DoLoadStuff method. This can be solved by modifying the calling method, like thus;
internal void DoLoadStuff()
{
int i = 9;
try
{
_contents = System.IO.File.ReadAllText(_filename);
}
catch(Exception e)
{
e.Data.Add("the object: ", this);
e.Data.Add("local i: ", i);
throw e;
}
} As you can see, it would be a lot of work to add "all" local variables for "each" method in the project. Adding a reference to the originating class might be worth the time to modify some of the key-classes.I are Troll
|
|
|
|
|
Thanks for the detailed response! I'm looking more into the data dictionary now and serializing the objects.
One other question. I notices after you throw add the values to the Data dictionary, you use
throw e; Doesn't this "reset" the stack track to the line where you rethrow the exception? Can you just use
throw; instead? Or would a simple throw not include your changes to the dictionary?
Thanks,
DybsThe shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
|
|
|
|
|
dybs wrote: Thanks for the detailed response! I'm looking more into the data dictionary now and serializing the objects.
My pleasure. Tracking the items in a dictionary would indeed be a nice alternative
dybs wrote: Doesn't this "reset" the stack track to the line where you rethrow the exception?
It does! That was sloppiness in my example.
dybs wrote: Or would a simple throw not include your changes to the dictionary?
It also includes the change if you omit the exception variable. Just verified it, just to be on the safe side.I are Troll
|
|
|
|
|