Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!orstcs!usenet!jacobs.CS.ORST.EDU!keffert From: keffert@jacobs.CS.ORST.EDU (Thomas Keffer) Newsgroups: comp.lang.c++ Subject: Re: Why is this program slow? / C++ versus C Performance Message-ID: <1991Jan12.225541.6709@usenet@scion.CS.ORST.EDU> Date: 12 Jan 91 22:55:41 GMT References: <1991Jan12.040453.6887@mentor.com> Sender: @usenet@scion.CS.ORST.EDU Distribution: usa Organization: Oregon State University, CS Dept. Lines: 42 Nntp-Posting-Host: jacobs.cs.orst.edu In article <1991Jan12.040453.6887@mentor.com> timh@.mentorg.com (Tim Harvey @ APD x1381) writes: >Peter Shirley posted the article (# 10640) "why is this program slow?" > >C++ is often charged with being an inherently slower language than C. > >A simple case was developed where a C++ program took almost twice as >long (about 170%) to run as a similar C program. It illustrates one >situation where C++ appears slower than C. Now hold on! We have done extensive testing of our C++ Math.h++ class library versus both C and Fortran and found only a very slight penalty that can very easily be recovered by using the more sophisticated algorithms made possible by the encapsulation, etc. properties of C++. Here's some hard data. The times shown are the times required to multiply two matrices together of the given size. The machine is a Sun 3/260 with a 68881 coprocessor. N**2 g++ -m68881 f77 -f68881 25 4.7s 3.9 100 16.4 15.5 400 64.2 62.1 You might want to implement a special "small matrix" class if you are going to be working with very small N (2, 3 or 4) matrices (e.g., graphics transformation matricies). But for larger N (5 and up), the time differences are completely trivial. The trick is careful attention to detail: use reference counting, minimize memory allocations, maintain a pool of the small (reference) structures, etc. -tk Disclaimer: I work for a company that vends a C++ math class. ___ Thomas Keffer Rogue Wave PO Box 2328 Corvallis, OR 97330 (503) 745-5908