Ektbear's Posts
Nairaland Forum › Ektbear's Profile › Ektbear's Posts
1 2 3 4 5 6 7 8 ... 135 136 137 138 139 140 141 142 143 (of 485 pages)
Eh. My point is, let the compiler do its job (optimize), and you do yours (code). Trying to optimize basic operations like multiplication, division, etc...not clear it makes a lot of sense. Your compiler knows more about your CPU than you do. |
haha i wish i could see all these hidden posts |
Highly unlikely to be GCC specific. Implement your own divide by 10 function, that divides an integer by 10, using whatever bitshift operations you like. I highly doubt it will outperform the compiler (whether GCC, Microsoft's compiler, etc)... |
Up Skeebanj |
Also, it doesn't make much sense to try to optimize division by say 10 using bit shifts. Like, your compiler will probably do a better job of figuring out the fastest way of optimizing things for your processor than you will. Like I said earlier, compilers these days are pretty smart.. |
Beaf: The bolded is almost correct. It is not a division in the sense that it only works for powers of 2. However, most divisions can be optimised by employing both bit shifts and multiplication, addition or subtraction; that is the reason for my last answer.Well, since the default in C, C++ and Java for integer/integer division seems to be floor afterwards, then technically doing: X / 2 and X >> 1 are the same thing. Of course in mathematics division of integers by 2 and right-shifting should not be the same. C++ is a weird language, lol |
But it is trivial to generalize the first code sample I gave to work for: a) Arbitrary-length integers b) Returning the Kth digit rather than the first one (left-most). If you understand the basic principle being used, you can easily generalize it. The problem with your proposal is that: a) It is probably slower b) It is more error-prone, as LordZouga's example shows c) It doesn't demonstrate that one actually understands certain things about math So if it were me grading it, I certainly wouldn't give bonus points. It is a very ugly solution. |
CyberG: ^^ Both of you are missing something: the exports to Asia would be at reduced profit to compensate for the longer distance required to transport the oil. Plus, the Chinese are guaranteed to want anything and everything for rock-bottom cheap prices!First point, agreed. More profitable for Nigeria to ship to the Eastern US rather than China. Alas. |
Bit shift twiddling is just division by (powers of ) 2. Or multiplication by 2. However, that isn't relevant in this case, since I'm not dividing or multiplying by powers of two. I'm dealing with powers of 10. |
Eh...that was an example so you guys get the pattern. The problem as specified, I know exactly how many digits the number I'm given has. It is 5. So no if statements are required. I didn't use any if statements in my initial code, did I? |
So if you have the integer: 7 And someone asks you for the left-most digit. What will you do? Will you convert to string, then read off the left-most element of the character array? No. You'll just return the number 7. What if you have the number 87? Will you convert to string? No. You divide by 10 (getting the result 8.7) and floor if necessary. (But in C++, evidently the flooring happens automatically, so just divide by 10.) Etc, etc... Converting to string makes zero sense. |
Beaf. You are missing the larger point I am making. If you have some function f(x) that converts an integer into its string representation, then necessarily it will have to do some sort of digit popping. The only way I know of doing this is through dividing by (powers of) 10. In fact, I suspect that it is impossible to convert arbitrary digit sequences into their string representations without some algorithm like this. It doesn't make any sense to rail against division, and then call some function that has division inside of it. So again...the key point is that converting sequences of digits to string will require division. Several times. |
The unfortunate reality I have to deal with is that 95%+ of the economic policies that right-wing parties like the Republicans propose/support I agree with. Alas, for other reasons I cannot identify with this party, and am a Democrat. How I wish the Republicans would focus less on this ugly social stuff they do so I can join them. All this "big government", "awoof for all" stuff that leftists promote...all it does is to slowly kill a country. |
I don't even understand the way you guys reason. You want to build a box which does this: Integer->[f(x)] -> Left-most digit Rather than doing it in the most direct way possible, you guys want to do: Integer->Convert to string representation->grab left-most character of string->convert this left-most character back to digit. Seems so unnecessary, roundabout, and wasteful. |
Which real-time systems don't allow you to perform division? ![]() So you can do integer to string conversion on a real-time system, but not division by 10? ![]() lol |
1. There is nothing "destructive" about division. Nothing is getting destroyed here.. 2. Division isn't particularly computationally expensive, to my knowledge. Same computational complexity as multiplication. And your processor will have specialized circuits to do it fairly efficienctly. 3. I don't know of any way to convert an X digit integer into a stream of characters without doing some sort of division. How on earth does one convert the number 12345 into the string "12345" without popping off digits? And the only way I know of to pop off digits is to do division. If you are calling some function that converts an integer into a string representation, under the hood it is probably doing a sequence of digit operations like that of the original function I had. |
Katsumoto: 1. After the devastation of WW2, it needed to support and reward its citizens for the war effort. Despite winning the war for Britain, Churchill lost to Labour's Clement Attlee in 1945 and Attlee used his 6 years to institute various welfarist programs. Despite, the conservatives returning to power for the next 15 years, Harold Wilson, another labour PM, would institute more welfare programs. It is instructive to note that the UK went broke twice under successive Labour governments in 1976 and 1979.Socialist, welfarist ideologies are one of the greatest evils that mankind has ever known. The end result of these welfarist policies was to make the UK weak and sickly, ripe for overtaking by more vibrant and industrious peoples (Germans and Japanese). |
Anyway, my opinion at this point: Germans and Japanese=>cool cats British => weaksauce Step up your game, UK ![]() |
Can socialism/welfarism be blamed alone, though? I was under the impression that Germany for example is pretty welfarist. France as well. Or was Germany more capitalistic from the 40s to the 70s (or whenever it was they overtook the UK)? |
1. Why did the UK become welfarist? 2. Why did they stop innovating? Is there something in British culture that is not supportive of science and technology? |
Heck. UK was the largest aid recipient, if I'm reading the table in the middle of the page correctly: http://en.wikipedia.org/wiki/Marshall_Plan |
Kats, it appears that the UK received money to rebuild after WW2 also. So it isn't even the case that the US gave France, Germany and Japan money to rebuild, and the UK got nothing. Somehow, they were just outcompeted and outmaneuvered after WW2. |
Right. But the question is, shouldn't: a) Overseas empire b) NOT getting your azz kicked in a war be enough to remain wealthier and maintain your competitive advantage over Germany and Japan? The Marshall program alone should not be enough to erase the two above advantages, no? What mistakes did the UK make from the 1940s to 1960s that caused it to lose its previous edge over Germany, France, and Japan? |
There is always going to be a special case in which Compiler X produces "better" code (in whichever way you define 'better') than Compiler Y. But the more important questions are: a) for my particular case, which compiler is best? b) which compiler typically performs better across most applications? Perhaps for the applications you deal with, there is no difference. That is fine. However, for many other applications of interest to people (http://stackoverflow.com/questions/1733627/anyone-here-has-benchmarked-intel-c-compiler-and-gcc), there is a difference, and Intel wins hands down. (b) is a more difficult question to answer and benchmark. However, my recommendation would be that anyone who has access to the latest Intel compiler for their system should use it over GCC. Commercial generically does not mean superior, yes. But one might expect that the compiler designed by the same guy who designed the chip might perform better than a compiler written by some third party. So if you can afford it, go with the Intel compiler. |
Interesting how much traffic threads like this get. So my question is, why does the UK have the least amount of "swag" of all the developed nations? Shouldn't it be far ahead of Germany and Japan, both of whom were defeated in wars and never had the advantage of centuries of overseas empire? UK even seems to be behind France. Why is the UK such a laggard? |
oyb: who knows we may even see a preponderance of gas powered carsInteresting article. I wouldn't be too worried about the long-term prices of oil and gas, though. China and India are not industrialized yet. And represent, what, 40% of humanity? So demand should grow enough over the next 20, 30 years for prices not to fall too much. It will be interesting to see how things turn out, though. |
[quote author=tpia@]hmph. ![]() sleeping with white women means the colour of skin is virtually insignificant?[/quote] ![]() |
My statement is correct. The portability of the GNU compilers is nice. But that doesn't mean that they produce code as efficient as Intel compilers. I highly doubt that GNU-compiled code is as efficient as that of any commercial compiler. Hence, the statement is correct. |
African democracy. Heh. Though I suppose that Ghana and Liberia are good counter-examples? |
oyb: who invited the afropseudoscientists /pseudo entrepreneurs /full time noisemakers to the discussions of professionals ?An excellent question. Anyway, interesting post texazzpete. |
Ruby as my main language since late 2009. Python for like 2-3 weeks now (trying to make it my main language). C++ and C in high school and a bit in college. Octave all throughout college and present. PHP for fun in high school. Took a class in Java in college. Oh, and of course, did some programming in BASIC at age 6 or 7 We used to have these book in the library that'd teach you how to get started programming in BASIC. They taught you the basic control structures.So pretty comfortable with many different languages and syntax styles. |
1 2 3 4 5 6 7 8 ... 135 136 137 138 139 140 141 142 143 (of 485 pages)


We used to have these book in the library that'd teach you how to get started programming in BASIC. They taught you the basic control structures.