![]() |
|
|
#1 |
|
Sep 2004
13·41 Posts |
in root_score.c for poly selection I was wondering why you used 65000 for 2^16? Its probably not a problem or bottleneck, but I rewrote the section to what I thought might be better...
Code:
/* find the largest power of p that does not exceed 2^16 */
max_power = 1;
power = p;
while (1) {
uint32 next_power = power * p;
if (next_power > 65000)
break;
power = next_power;
max_power++;
}
Code:
/* find the largest power of p that does not exceed 2^16 */
max_power = 1;
power = p;
uint32 next_power = power * p;
while (next_power <= ushort.maxvalue) { //works in c# don't know about c
max_power++;
power = next_power;
next_power *= p;
}
Last fiddled with by Joshua2 on 2009-04-04 at 22:40 |
|
|
|
|
|
#2 |
|
"Robert Gerbicz"
Oct 2005
Hungary
27148 Posts |
No improvement, because there is no perfect prime power in the (65000,65536) interval.
|
|
|
|
|
|
#3 |
|
Sep 2004
13×41 Posts |
Ok, but I learned in programming class to avoid using break statements when possible. Since there are only a few primes, it seems they could be hard-coded, but that probably wouldn't save much time.
|
|
|
|
|
|
#4 | |
|
Jan 2004
103 Posts |
Quote:
A sample satisfying Joshua2's "avoid break's and goto's" principle (and not a principle I blindly adhere to I might add, especially where performance is important): Code:
/* find the largest power of p that does not exceed 2^16 */
max_power=1;
power=p;
while (power<=SHRT_MAX && (next_power=p*power)<=USHRT_MAX) {
power=next_power;
max_power++;
}
|
|
|
|
|
|
|
#5 |
|
Sep 2004
13×41 Posts |
Doesn't that have twice as many comparisons as necessary? When will "(next_power=p*power)<=USHRT_MAX)" be true and "power<=SHRT_MAX" be false?
|
|
|
|
|
|
#6 |
|
"Serge"
Mar 2008
Phi(4,2^7658614+1)/2
100101000001012 Posts |
"On the internet, nobody knows that you are a dog."
<== Is that better? Social mimicry was good for a while. Now it's time to reveal my real self. "avoid break's and goto's" principle is good for beginners. It is very true that this how most programmers should be taught. Then, if/when they get to the next level, they will be told (or they will tell themselves) that all has to be forgotten and learned from scratch. The serious debate would be long (I mean it is long, tons of stuff can be found about it), so let's just leave it at that. Look at the lasieve code. Shocking! Not just gotos, but setjmp's are used. How amateurish! (Of course not.) Same with physics. Many times, students will hear: "forget all you've learned in the last class and all classes before." ...but those who didn't get into this class will in fact do better with that other, easy physics that they learned earlier. Same with chess. First class: queen moves like this, rook moves like that, and never put you pieces under attack. It would be silly to analyze Alekhin-Capablanca or whatever in the first class; "why did he put his queen here? it's under attack" questions will follow. Or more closer to this example, why did he move like that - right here, in this move? makes no sense! ...because he was concentraing on the attack to follow in ten moves, and this paricular move upon consideration had no significance at all - so don't drill into it. He spent no time thinking about this move, and one day, so will you. (Alas, I never will. I am hopeless here.) Very few parts of the code need all optimization you can get (including unimaginable), and the bulk don't and should be readable, that's all. My 2 cents. |
|
|
|
|
|
#7 |
|
"Nancy"
Aug 2002
Alexandria
2,467 Posts |
I remember hearing a quote somewhere, but don't remember the source. It went something like: "Mastering an art takes twenty years. Ten years to learn all the rules, then another ten to learn when to break them."
Alex |
|
|
|
|
|
#8 | |
|
"Mark"
Apr 2003
Between here and the
11·577 Posts |
Quote:
One thing that does annoy me a little is the use of "int" in code that is intended to be compiled across multiple platforms where the size varies from platform to platform. I often find that developers who presume that an int is always 32 bits only to find that code breaks on platforms where it is 16 bits. I prefer using the defines in stdint.h to help avoid these issues. It also helps avoid some issues that might occur when using a 32-bit or 64-bit compiler switch. The code should work correctly regardless of the switch used. |
|
|
|
|
|
|
#9 | |
|
Dec 2008
179 Posts |
Quote:
Well that's just stupid. Each function should be divided into two, with the outer function just calling the inner one and doing the tracing, and the inner returning to the outer one wherever necessary; also, the outer functions should be invocations of a macro or a template. |
|
|
|
|
|
|
#10 | |
|
"Serge"
Mar 2008
Phi(4,2^7658614+1)/2
36·13 Posts |
Quote:
I like this opinion: C in the first course considered harmful |
|
|
|
|
|
|
#11 |
|
Jan 2004
103 Posts |
No. The first inequality is the early exit, saving 1 multiply (not a big savings), the second inequality only gets invoked if the first inequality is true - that's how C evaluates AND expressions. In the C language the scenario you suggested will never be evaluated, so all of the primes above USHRT_MAX will exit the loop without having to do the trial multiply by p.
Last fiddled with by tmorrow on 2009-04-05 at 21:31 |
|
|
|
![]() |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Ivy Bridge Thread | Dubslow | Hardware | 70 | 2012-05-25 00:56 |
| Bad Poetry Thread | Passionflower2 | Lounge | 26 | 2012-01-25 04:35 |
| PRP discussion thread | philmoore | Five or Bust - The Dual Sierpinski Problem | 83 | 2010-09-25 10:20 |
| If a p-1 thread is unStickied, does it mean it's done? | jasong | Marin's Mersenne-aries | 2 | 2006-10-08 02:07 |
| Deutscher Thread (german thread) | TauCeti | NFSNET Discussion | 0 | 2003-12-11 22:12 |