smart use of std:algorithm

This is a discussion on smart use of std:algorithm within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by master5001 What makes you think your compiler is not aware of every opcode for your CPU and ...

  1. #16
    EOP
    EOP is offline
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    Quote Originally Posted by master5001 View Post
    What makes you think your compiler is not aware of every opcode for your CPU and knows how to rearrange data to be optimal for loops.
    Maybe looking at the asm code?

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by EOP View Post
    What about e.g. using loopnz instead of compiler generated loops?
    What processor are we talking about? Your point may be valid for processors up to 386.

    Using loopnz in modern x86 is not faster than a well-written loop using separate decrement and jnz operations. It may not be slower either, but using the generic version is still better, since the compiler can generate the loop using ANY register, rather than being forced to use ECX. Restricting register usage will make the register allocation more complicated (and the compiler will have to know at the beginning that it should use loopnz, so that when it gets to the end, it has the right value in ECX). In this case, there's no benefit. If something like this makes loops MUCH more efficient, then the compiler will be tuned to do that, but modern x86 processors are very efficient at simple instructions, so in most cases, the complex alternative instructions are either slower or equal to the simple variants.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Perhaps the loops were slower due to a misunderstanding of tabstop's algorithm? In particular, although tabstop says there's nothing wrong with nested loops, his algorithm doesn't actually have any - just a single loop that goes through both containers at the same time, taking advantage of their being sorted and unique.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    I myself have previously needed to do this, and not long ago I might add. set_symmetric_difference didn't cut it because it puts both differences in the same set. set_difference works but needs two passes.
    The approach I suggest is to go into your algorithm header, find the code for set_symmetric difference, copy it and modify it to put the A-B and B-A into seperate sets.
    It logically should be about twice as fast as two set_difference calls. If not then you've probably made a mistake.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which smart pointer to use?
    By leeor_net in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2009, 04:29 AM
  2. smart pointer release
    By George2 in forum C++ Programming
    Replies: 24
    Last Post: 02-13-2008, 07:51 AM
  3. can not create Visual C++ Smart Device project
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 11-26-2006, 05:20 AM
  4. Intelligent vs Smart
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 07-18-2004, 01:42 PM
  5. Using a smart pointer for all objects
    By phalc in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2004, 08:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21