For testing solutions , is codechef using a 64 bit machine or a 32 bit machine. I am just curious does it make a difference in a computationally intensive solution.
If it wasn’t 64 bit, would it support 64-bit data types?
Codechef uses SPOJ Online Judge
http://www.codechef.com/wiki/environment-details
EDIT:
Thanks for the quick ans.
#include <ctime>
#include <iostream>
using namespace std;
int main() {
{
clock_t startInt = clock();
int a = 0;
for (int i = 0; i < 2147483647; i++) {
a++;
}
clock_t endInt = clock();
cout << "Time: for int " << a << " " << (double) (endInt - startInt) / CLOCKS_PER_SEC << endl;
clock_t startLong = clock();
long long b = 0;
for (long long i = 0; i < 2147483647; i++) {
b++;
}
clock_t endLong = clock();
cout << "Time: for long " << b << " " << (double) (endLong - startLong) / CLOCKS_PER_SEC << endl;
}
{
clock_t startInt = clock();
int a = 0;
for (int i = 0; i < 1000000; i++) {
a++;
}
clock_t endInt = clock();
cout << "Time: for int " << a << " " << (double) (endInt - startInt) / CLOCKS_PER_SEC << endl;
clock_t startLong = clock();
long long b = 0;
for (long long i = 0; i < 1000000; i++) {
b++;
}
clock_t endLong = clock();
cout << "Time: for long " << b << " " << (double) (endLong - startLong) / CLOCKS_PER_SEC << endl;
}
}
Can I expect the same behavior ? Pardon my ignorance
You might be looking for long long
. size of long and int can be same, nowhere in manual it is mentioned that long should be greater in size than int. It’s just that sizeof(short) <= sizeof(int) <= sizeof(long)
Be more specific in your question. What is your confusion? Indent your code properly, or paste it on ideone/pastebin and refer a link here
I wonder your code would even compile. You have not declared that you will be using namespace std
using namespace std --> I typo
If you want to know the time spent in seconds, then you must divide the difference by CLOCKS_PER_SEC. After doing that, I am getting same time 5.44 and 5.45 for the first two cases. Now what is your problem?
What is your environment of testing ? Updated the code a bit. Are you testing on 64bit os ? I am using 32 bit vista
Can you tell me where did you run the code. The OS,Compiler etc ?
I am using Ubuntu on my 64 bit machine, gcc 4.8.1. Attached a screenshot in my answer
And now, I guess you have found the problem. It’s in your compiler. Read the answer, the link that I have shared
#include
#include
using namespace std;
void test(int end) {
clock_t startInt = clock();
int a = 0;
for (int i = 0; i < end; i++) {
a++;
}
clock_t endInt = clock();
cout << "int " << a << " sizeof " << sizeof(int) << " " << (double) (endInt - startInt) / CLOCKS_PER_SEC << endl;
clock_t startLong = clock();
long b = 0;
for (long i = 0; i < end; i++) {
b++;
}
clock_t endLong = clock();
cout << "long " << b << " sizeof " << sizeof(long) << " " << (double) (endLong - startLong) / CLOCKS_PER_SEC << endl;
clock_t startLongLong = clock();
long long c = 0;
for (long long i = 0; i < end; i++) {
c++;
}
clock_t endLongLong = clock();
cout << "long long " << c << " sizeof " << sizeof(long long) << " " << (double) (endLongLong - startLongLong) / CLOCKS_PER_SEC << endl;
}
int main() {
test(2147483647);
test(1000000);
}
int 2147483647 sizeof 4 11.965
long 2147483647 sizeof 4 11.716
long long 2147483647 sizeof 8 27.238
int 1000000 sizeof 4 0.015
long 1000000 sizeof 4 0
long long 1000000 sizeof 8 0.016
Probably Depends on env (compiler processor etc) Which one is the best for loop counters on SPOJ ?
are you asking for the best data type? It depends on the problem, the constraints.
For counter till one million or more which ones faster on SPOJ?
as long as you are below 10^9, you can safely use int