Thread: How to get number of processor cycles since start up

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    26

    How to get number of processor cycles since start up

    Hi,
    I'm looking to make a random number generator and want to multiply the number generated by the C random number generator by the number of processor cycles there has been since start up. Is there a way to get the number of processor cycles there has been in C?


    Thanks in advance.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think there's such a function. It would be incredibly unreasonable.
    Are you sure you don't mean the time since start up?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Not for the CPU, although you can get such information from your graphics card, although it will be in GPU clock cycles, which are usualyl much slower than the CPU.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Since the startup of what
    - your program
    - your machine
    ?

    I can imagine the time since program start might be fairly constant, which isn't going to help you much.

    You should also know that
    srand( time(NULL) );
    isn't going to be any less random than
    srand( exoticTimeFunction() );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Don't know what you want to do, but your idea doesn't sound really great. The standard rand() function will do the job in (i would say) at least 99% of the case. If what you are looking for is creating a random number generator who can generates numbers on a larger interval than 0 - RAND_MAX, then there might be better way to do so than multiplying the result of rand() by the number of clock cycle since the start of the program/machine. I guess it could work, but this could also be unreliable.

    Also, if you want to know, you can get the number of clock cycles since the processor is turned on by using the RDTSC instruction (if you are running on a IA-32 or Intel 64 architecture processor). If you want more information about using this instruction, you can refer to this link (warning for slow connection: 9 MB PDF).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Learning Memory, Ins and Outs?
    By Zoiked in forum C Programming
    Replies: 1
    Last Post: 08-27-2007, 04:43 PM
  4. Replies: 4
    Last Post: 03-09-2002, 01:22 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM