Sponsored Content
Full Discussion: Byte swap timing
Top Forums Programming Byte swap timing Post 302975261 by migurus on Thursday 9th of June 2016 09:25:45 PM
Old 06-09-2016
Byte swap timing

I have noticed the difference in byte swap timing between two Ubuntu systems. The bswap_32 used to work just fine on the old system, but on the new one it lags behind home-grown swap.

My code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <byteswap.h>
#include <sys/time.h>
 union   _U {
          unsigned int          in;
          struct _CH {
                  unsigned char c[4];
          } CH;
};
 int main(int argc, char *argv[])
{
  struct timeval t1, t2;
   union _U u;
  int   n;
  unsigned char tmp;
  
         u.in = (argc == 2) ? atoi(argv[1]) : 0xff;
         gettimeofday(&t1, NULL);
        for(n = 0; n < 100 * 1000000; n++)
        {
                u.in = n;

                 // Version 1
                u.in = bswap_32(u.in);

                 // Version 2 (homegrown)
                //tmp = u.CH.c[0];
                //u.CH.c[0] = u.CH.c[3];
                //u.CH.c[1] = u.CH.c[2];
                //u.CH.c[2] = u.CH.c[1];
                //u.CH.c[3] = tmp;
        }
        gettimeofday(&t2, NULL);
        printf("%lu mls\n", 
                (t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec));
        return(0);
}

I compile it on both systems simply as
Code:
cc -g

+++

Old system : ProLiant ML350 G4 / Intel(R) Xeon(TM) CPU 3.20GHz (about 8 years old)
Linux OLD 2.6.24-32-server #1 SMP Thu Jul 12 15:21:48 UTC 2012 i686 GNU/Linux
gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu4)

Version 1 avg 500,000 mls, version 2 avg 1,250,000 mls

+++

New system : ProLiant DL360p Gen8 / Intel(R) Xeon(R) CPU E5-2609 0 @ 2.40GHz 4 cores (about 3 years old)
Linux NEW 3.13.0-55-generic #94-Ubuntu SMP Thu Jun 18 00:27:10 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2)

Version 1 avg 480,000 mls, version 2 avg 390,000 mls

+++

Why on the new system a standard bswap_32 is quite a bit slower than manually shuffling the bytes?

Thanks in advance.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

timing a loop in unix.

hi guys.. i have a shell script that loops through a certain directory to see if a file has been created and then prints the file if it exists... the only problem i have now is that sometimes the flat does not get created by the program thats supposed to create it, in this scenario, my loop... (1 Reply)
Discussion started by: wolkott
1 Replies

2. UNIX for Advanced & Expert Users

scp timing out

having problems using scp in that during peak hours it appears to time out. anyone have similar experiences? any thoughts regarding a solution... (1 Reply)
Discussion started by: jph
1 Replies

3. Shell Programming and Scripting

Timing out a SSH

I need to make it so an autmated process which involves ssh, times out if ssh prompts for a password. Most of the time it shouldnt prompt for a password. But if it does i need it to time it out or get a status and stop the ssh and log that the ssh failed and move onto the next server. Is there any... (9 Replies)
Discussion started by: rcunn87
9 Replies

4. Programming

timing your functions

hi everyone. If you have a function created in your code and you want to find out how long it takes for it to run you can use a struct called gettimeofday(). so lets say we have a function like this int myfunction (int r) { /*some math calculations*/ return answer; } How do i set up... (3 Replies)
Discussion started by: bebop1111116
3 Replies

5. Solaris

Swap config - Mirror swap or not?

Hello and thanks in advance. I have a Sun box with raid 1 on the O/S disks using solaris svm. I want to unmirror my swap partition, and add the slice on the second disk as an additional swap device. This would give me twice as much swap space. I have been warned not to do this by some... (3 Replies)
Discussion started by: BG_JrAdmin
3 Replies

6. Shell Programming and Scripting

Check if 2 files are identical byte-to-byte?

In my server migration requirement, I need to compare if one file on old server is exactly the same as the corresponding file on the new server. For diff and comm, the inputs need to be sorted. But I do not want to disturb the content of the file and need to find byte-to-byte match. Please... (4 Replies)
Discussion started by: krishmaths
4 Replies

7. Shell Programming and Scripting

Remove a byte(Last byte from the last line)

Hi All Can anyone please suggest me how to remove the last byte from a falt file .This is from the last line's last BYTE. Please suggest me something. Thank's and regards Vinay (1 Reply)
Discussion started by: vinayrao
1 Replies

8. HP-UX

Swap device file and swap sapce

Hi I have an integrity machine rx7620 and rx8640 running hp-ux 11.31. I'm planning to fine tune the system: - I would like to know when does the memory swap space spill over to the device swap space? - And how much % of memory swap utilization should be specified (swap space device... (6 Replies)
Discussion started by: lamoul
6 Replies

9. Shell Programming and Scripting

Timing a script

i have a very big script i have that i'd like to add a timeout to. this script runs on a several remote host. i update this script with timeout clause and then copy it over to all the hosts on which it is currently on. basically, i want the timeout to make the script abort/exit if it's... (1 Reply)
Discussion started by: SkySmart
1 Replies

10. Solaris

Explain the output of swap -s and swap -l

Hi Solaris Folks :), I need to calculate the swap usage on solaris server, please let me understand the output of below swap -s and swap -l commands. $swap -s total: 1774912k bytes allocated + 240616k reserved = 2015528k used, 14542512k available $swap -l swapfile dev swaplo... (6 Replies)
Discussion started by: seenuvasan1985
6 Replies
ggCurTime(3)								GGI							      ggCurTime(3)

NAME
ggCurTime, ggUSleep, ggUSlumber - Portable Time Routines SYNOPSIS
#include <ggi/gg.h> int ggCurTime(struct timeval *tv); int ggUSleep(int32_t usecs); void ggUSlumber(int32_t usecs); DESCRIPTION
ggCurTime fills the timeval structure pointed to by tv with the current time to the best precision available on the executing platform. ggUSleep sleeps for at least usecs microseconds, to the best precision available on the executing platform, but may be woken up by a signal or other unspecified condition. It is not guaranteed that ggUSleep will wake up prematurely for any specific reason. It is mainly useful for points where the main objective is to avoid using CPU resources, not to perform accurate timing. ggUSlumber does the same thing as ggUSleep, but is guaranteed not to return until the allotted time has elapsed. It is slightly less effi- cient than ggUSleep with reguard to CPU utilization. All times represent wall-clock (real, versus processor) times. The above routines are often simple macros rather than functions, and as such should not be used by reference. The above functions are threadsafe, but are not guaranteed to be safe to use in a thread that may be cancelled during their execution. They are also not guaranteed to be safe to use in special contexts such as LibGG task handlers, signal handlers and asyncronous procedure calls. RETURN VALUE
ggCurTime returns GGI_OK on success, or a negative value on failure. On Windows, this function will never report a failure. On platforms where gettimeofday(2) is used, the error code is the one returned by gettimeofday. ggUSleep returns GGI_OK when the alloted time interval has elapsed, or a non-zero value if the sleep was interrupted. On platforms where usleep(3) is used, the error code is the one return by usleep. EXAMPLE
A demonstration on how to measure a framerate. struct timeval start, stop, diff; long time_of_frame = 1; int framerate; ... ggCurTime(&start); /* do something here, i.e. render and display a frame */ ggCurTime(&stop); diff.tv_sec = stop_tv.tv_sec - start_tv.tv_sec; diff.tv_usec = stop_tv.tv_usec - start_tv.tv_usec; if (diff.tv_usec < 0) { diff.tv_usec += 1000000; diff.tv_sec--; } time_of_frame = diff.tv_sec * 1000 + diff.tv_usec / 1000; if (time_of_frame == 0) time_of_frame = 1; /* CPU too fast? */ printf("framerate: %i ", 1000 / time_of_framerate); SEE ALSO
gettimeofday(2), usleep(3) libgg-1.0.x 2005-08-26 ggCurTime(3)
All times are GMT -4. The time now is 02:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy