probably doing something fundamentally wrong , but im used to the luxury of java's System.currentTimeMillis() or System.nanoTime() unfortunately. i have a code that i posted before for a different problem (couldnt compile properly in pelles C) , but now , although its compiling well and good , my clock outputs always give zero. any help here ?

my code :

#include<stdio.h>
#include<time.h>
int main(void){
    long long int i = 0, y =0 , x = 7985463214932541; // x  = a very big odd number
    clock_t t1,t2;
    printf("\n strating bitwise method :");
    t1 = clock();
    printf("\nstarting time : %ld\n",t1);
    for(i = 0 ; i < 500000000 ; i++){
        if(x&1) y=1;
    }
    t1 = clock() - t1;
    printf("bitwise time : %15ld\n",t1);
    y=0;
    printf("\n strating mul div method : \n");
    t2 = clock();
    printf("starting time : %ld\n",t2);
    for(i = 0; i < 500000000 ; i++){
        if(((x/2)*2) != x ) y=1;
    }
    t2 = clock() - t2;    
    printf("\nmul-div time: %15ld\n\n",t2);
    return 0;
}

Recommended Answers

All 9 Replies

Member Avatar for Rahul47

Are you expecitng something more than this:
a6192ce7b6685ccdec803a5b96bd09f4

yes , something like that ! though i doubt the starting time of 19. how did you get it ?

Member Avatar for Rahul47

I first thought it was due to x which was of long long type . . so checked its range, but it was fine.

how did you get it ?

Dev C++

I guess you are using pelles C.

i modifiedmy code somewhat , but still no go

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<stdint.h>
clock_t startm, stopm;
#define START if ( (startm = clock()) == -1) {printf("Error calling clock");exit(1);}
#define STOP if ( (stopm = clock()) == -1) {printf("Error calling clock");exit(1);}
#define PRINTTIME printf( "%ju ticks used by the processor.", (uintmax_t)(stopm-startm));
#define COUNT 18446744073709551600
#define STEP COUNT/100

int timetest(void){
    unsigned long long int i = 0, y =0 , x = 76546546545541; // x  = a random big odd number
    clock_t startTime,stopTime;
    printf("\nstarting bitwise method :\n");
    START;
    for(i = 0 ; i < COUNT ; i++){
        if(x&1) y=1;
    }
    STOP;
    printf("\n");
    PRINTTIME;

    y=0;
    printf("\nstarting mul-div method :\n");
    START;  
    for(i = 0; i < COUNT ; i++){     
        if(((x/2)*2) != x ) y=1;
    }
    STOP;
    printf("\n");
    PRINTTIME;
    printf("\n\n");
    return 0;
}

im always getting 0 ticks used by the processor. as the output.

Member Avatar for Rahul47

Did you tried it in Dev C++ ? Coz i used pelles C and sometimes it was not even compiling simple programs.

does dev c/c++ support c11 ?

Member Avatar for Rahul47

According to this article nope ! They suggested to switch to Orwell Dev C++ which Supports C11.

So, lets make a switch and see.

iv have enough of compiler issues.
created a java version of the above program. gives me answers. though its for the java platform.

public class test {
    private final static int count = 500000000;
    private final static long num = 55465465465465L;
    private final static int loops = 25;
    private long runTime;
    private long result;
    private long bitArr[] = new long[loops];
    private long mulDivArr[] = new long[loops];
    private double meanVal;

    private void bitwiser() {
        for (int i = 0; i < count; i++) {
            result = num & 1;
        }
        // System.out.println("run time for bitwise op : " + runTime);
    }

    private void muldiv() {
        for (int i = 0; i < count; i++) {
            result = (num / 2) * 2;
        }
        // System.out.println("run time for muldiv op : " + runTime);
    }

    public test() {
        // run loops and gather info
        for (int i = 0; i < loops; i++) {
            runTime = System.currentTimeMillis();
            bitwiser();
            runTime = System.currentTimeMillis() - runTime;
            bitArr[i] = runTime;
            runTime = System.currentTimeMillis();
            muldiv();
            runTime = System.currentTimeMillis() - runTime;
            mulDivArr[i] = runTime;
        }
        // calculate stats
        meanVal = stats.mean(bitArr);
        System.out.println("bitwise time : " + meanVal);
        meanVal = stats.mean(mulDivArr);
        System.out.println("muldiv time : " + meanVal);

    }

    public static void main(String[] args) {
        new test();
    }
}

final class stats {
    private stats() {
        // empty
    }

    public static double mean(long[] a) {
        if (a.length == 0)
            return Double.NaN;
        long sum = sum(a);
        return (double) sum / a.length;
    }

    public static long sum(long[] a) {
        long sum = 0L;
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
        }
        return sum;
    }

}

output :

bitwise time : 1109.52
muldiv time : 1108.16

on average , bitwise seems to be a tad slower than muldiv.

Member Avatar for Rahul47

Just downloaded Dev C++ 5.4.2 and it works fine . . . some of my programs which were earlier identified as THREAT are also executed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.