![]() ![]() (Note: untested code if it doesn't work, try t=2 or 8.) The basic structure of Hensel's lemma is the following. (I don't think it's applicable directly, but it works with some modifications.) Before doing that, I divide out all powers of 2 with a binary search:Īt this point, for our number to be a square, it must be 1 mod 8. Finally, try to compute the square root using a method similar to Hensel's lemma.However, I just use a table of size 512 To actually check if the residue is a square, I look up the answer in a precomputed table. At this point, y is between 0 and 511. (In reading the code below, note that my input is int64 x.) (I found looking at the last six didn't help.) I also answer yes for 0. This includes negative numbers and looking at the last 4 bits. Your results may vary, especially because I don't know how the Java factor will play out. I figured out a method that works ~35% faster than your 6bits+Carmack+sqrt code, at least with my CPU (x86) and programming language (C/C++). Int value = 1 // note that we start at 1 since we're counting TObjectIntHashMap freq = new TObjectIntHashMap() Map.putIfAbsent(word, new AtomicLong(0)) Int count = ntainsKey(word) ? freq.get(word) : 0 ĪtomicLong import Here is the crucial code from each method. So unless I discover problems with it, that's the way I'm most likely to go. Personally, I find the MutableInt method the most attractive, since it doesn't require loading any third-party classes. I'd be happy to hear from anybody who has good insights into how the MutableInt and Trove methods would be likely to affect memory usage. Note that I haven't profiled memory usage in the different scenarios. I also ran TestForNull with final variables, but the difference was negligible. However, if threading is an issue, AtomicLong might be more attractive than the others (I'm not really sure). It would appear that only the MutableInt method and the Trove method are significantly faster, in that only they give a performance boost of more than 10%. MutableInt: 25.747 seconds (1.19 times as fast). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |