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 7595399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:39:51+00:00 2026-05-30T21:39:51+00:00

which of the two is faster: ? 1. char* _pos ..; short value =

  • 0

which of the two is faster: ?

1.

char* _pos ..;
short value = ..;

*((short*)_pos = va;

2.

char* _pos ..;
short value = ..;

memcpy(_pos, &value, sizeof(short));
  • 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-30T21:39:51+00:00Added an answer on May 30, 2026 at 9:39 pm

    As with all “which is faster?” questions, you should benchmark it to see for yourself. And if it matters, then ask why and pick which you want.

    In any case, your first example is technically undefined behavior since you are violating strict-aliasing. So if you had to choose without benchmarking, go with the second one.


    To answer the actual question, which is faster will probably depend on the alignment of pos. If it’s aligned properly, then 1 will probably be faster. If not, then 2 might be faster depending on how it’s optimized by the compiler. (1 might even crash if the hardware doesn’t support misaligned access.)

    But this is all guess-work. You really need to benchmark it to know for sure.
    At the very least, you should look at the compiled assembly:

    :     *(short *)_pos = value;
    
    mov WORD PTR [rcx], dx
    

    vs.

    :     memcpy(_pos, &value, sizeof(short));
    
    mov WORD PTR [rcx], dx
    

    Which in this case (in MSVC) shows the exact same assembly with default optimizations. So you can expect the performance to be the same.

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

Sidebar

Related Questions

Which of these two statements is faster/better practice? myList.Where(x => { bool itemOne= x.ItemOne
I have to compare two strings for case insensitive equality which one is faster
Which of the two following queries might be faster? $cposts = mysql_query(SELECT id FROM
Which of these two is better/faster? var a = $('.listItem', $('#myList')); vs var a
Which is the faster way (in character level) when checking if two strings or
I have a table with two indexes, one of which is the faster covering
Which of the two will be faster and how can do such measurements foreach
Does anyone know which of the two locking constructs is faster? I have: private
Two Parts two my question. Which is more efficient/faster: int a,b,c,d,e,f; int a1,b1,c1,d1,e1,f1; int
I have created a form on which two components are present, button and progressbar

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.