Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7648129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:38:13+00:00 2026-05-31T10:38:13+00:00

From what I have read, and the way I understand how memory structures work,

  • 0

From what I have read, and the way I understand how memory structures work, the vector creates an array where all the values are aligned after each other, while the linked list I have put together below will point to a random point in memory where the next object is allocated. Hence the vector should be faster for this code.

When I compile the following code I get runtimes of 4 sec for the linked list, while the vector runs in 21 sec, thats a 5 times speed increase for the vector.

Am I missing something here? Or does anyone have an explanation for this?

#include <vector>
#include <iostream>
#include <ctime>

using namespace std;

struct point{
    float t;
    float v;
};

struct pointlist{
    float t;
    float v;
    struct pointlist *next;
};

void fillpoints(std::vector<struct point> &points, struct pointlist *pointlist) {
    int i;

    for (i = 0; i<8;i++) {
        struct point newpoint; // Fill vector
        newpoint.t = (float)i;
        newpoint.v = (float)i/2;
        points.push_back(newpoint);

        pointlist->t = (float)i; // Fill linked list
        pointlist->v = (float)i/2;
        struct pointlist *temppoint = (struct pointlist *)malloc(sizeof(struct pointlist));
        pointlist->next = temppoint;
        pointlist = temppoint;
    }

    struct point newpoint;
    newpoint.t = 0;
    newpoint.v = (float)i/2;
    points.push_back(newpoint);
    pointlist->t = 0;
    pointlist->v = (float)i/2;
    pointlist->next = NULL;
}

float areavec(std::vector<struct point> pointlist) {
    float y1, z1, y2, z2, area;

    area = 0;
    y2 = pointlist[0].t - pointlist[0].v;
    z2 = pointlist[0].t + pointlist[0].v;

    for (unsigned int i = 1; i<pointlist.size();i++) {
        y1 = y2;
        z1 = z2;
        y2 = pointlist[i].t - pointlist[i].v;
        z2 = pointlist[i].t + pointlist[i].v;
        area += (y1*z2) - (y2*z1);
    }
    return area;
}

float arealist(struct pointlist *pointlist) {
    float y1, z1, y2, z2, area;

    area = 0;
    y2 = pointlist->t - pointlist->v;
    z2 = pointlist->t + pointlist->v;

    while (pointlist->next != NULL) {
        y1 = y2;
        z1 = z2;
        y2 = pointlist->next->t - pointlist->next->v;
        z2 = pointlist->next->t + pointlist->next->v;
        area += (y1*z2) - (y2*z1);

        pointlist = pointlist->next;
    }
    return area;
}

void main() {
    int runs = 200000000;
    float a;
    std::vector<struct point> points;
    struct pointlist *pointlist = (struct pointlist *)malloc(sizeof(struct pointlist));

    struct pointlist *test = pointlist;
    fillpoints(points, pointlist);

    clock_t start = clock();
    for (int i=0; i<runs; i++) {
        a = arealist(pointlist);
    }
    cout << "Time: " << (clock()-start)/CLOCKS_PER_SEC << " Area: " << a << endl;

    start = clock();
    for (int i=0; i<runs; i++) {
        a = areavec(points);
    }
    cout << "Time: " << (clock()-start)/CLOCKS_PER_SEC << " Area: " << a << endl;

    cin.get();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:38:15+00:00Added an answer on May 31, 2026 at 10:38 am
    float areavec(std::vector<struct point> pointlist)
    

    Here, I’ll fix that:

    float areavec(const std::vector<struct point>& pointlist)
    

    You were copying the vector 200000000 times. Whereas with the linked list, you were just copying a pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code to read from a pdf file. Is there a way
I have read about session fixation and from what I understand it forces a
I have read from some article that say's Apple doesn't approve the application which
From what I have read best practice is to have classes based on an
I have read (or perhaps heard from a colleague) that in .NET, TransactionScope can
I have read the following properties from AD, TerminalServicesProfilePath TerminalServicesHomeDirectory TerminalServicesHomeDrive I've tried DirectoryEntry
I have read this article from High Scalability about Stack Overflow and other large
I have read several documentations about building custom ErrorHandler (by inheriting from IErrorHandler). Unfortunately,
From the .net 4.0 previews I have read until now there has been lots
I have just switched from svn to mercurial and have read some tutorials about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.